Re: [go-nuts] How to cast *interface{} into *T ?
Thanks guys. I have finished my homework thanks to you: https://pkg.go.dev/github.com/fairking/di_plus On Friday, 25 November 2022 at 16:35:50 UTC pattnaik...@gmail.com wrote: > If you create a wrapper interface type and create *generic constraints* with > the *MyWrapper* struct then it'll type cast with *interface* with *(T)*. > > https://go.dev/play/p/-VnQxG77sDL > > > On Fri, Nov 25, 2022 at 3:38 PM 'Axel Wagner' via golang-nuts < > golan...@googlegroups.com> wrote: > >> If you assign a value to an interface, it will get copied. That's the >> same as if you assign an int to another int - it will get copied. If you >> want the interface to contain a reference, you have to assign a pointer to >> it: https://go.dev/play/p/nLw51pjWh4u >> >> Side note: Go doesn't have "instances". It has "variables", which is >> *probably* what you mean. >> >> On Fri, Nov 25, 2022 at 10:55 AM Denis P wrote: >> >>> Thank you everyone for your help. >>> The problem still exists. >>> I am looking for a solution where s and s2 point to the same instance. >>> The current example proves they are different instances. >>> https://go.dev/play/p/_JyfJelhIy4 >>> >>> The main goal is to store any type in a wrapper and then get the >>> reference of the actual struct. >>> Also it must be the same instance, so the println should print 2, 2, 2 >>> >>> But look like there is no obvious solution. :-( >>> >>> On Friday, 25 November 2022 at 08:19:17 UTC Brian Candler wrote: >>> >>>> To give a real-world example: >>>> >>>> https://pkg.go.dev/github.com/prometheus/client_golang/prometheus >>>> >>>> type metrics struct { >>>> cpuTempprometheus.Gauge >>>> hdFailures *prometheus.CounterVec >>>> } >>>> >>>> Question: why is prometheus.Gauge not a pointer, but >>>> *prometheus.CounterVec is a pointer? >>>> >>>> Answer: because prometheus.Gauge is an interface, whereas >>>> prometheus.CounterVec is a struct. >>>> >>>> An interface essentially already *contains* a pointer to a data value - >>>> in fact, a tuple of (type, pointer). So you should never take a pointer >>>> to >>>> an interface. Just pass the interface value, and it will copy the (type, >>>> pointer) pair. >>>> >>>> However, when you extract a value from an interface, it *will* always >>>> copy the internal value. This avoids aliasing issues: someone who passes >>>> an interface value to someone else, won't expect the recipient to be able >>>> to change the sender's copy. >>>> >>>> e.g. >>>> https://go.dev/play/p/u5y3Kwm9Ydz >>>> >>>> Of course, if your interface *contains* a pointer, then the recipient >>>> of the interface can modify the thing being pointed to. >>>> https://go.dev/play/p/o_XAJtNuyGF >>>> >>>> But they can't modify the pointer itself held within the interface, to >>>> make it point to something else. >>>> >>>> The short version is: avoid pointers to interfaces, as the FAQ says. >>>> Instead, let a concrete pointer value satisfy an interface. >>>> >>>> On Friday, 25 November 2022 at 07:40:59 UTC Nigel Tao wrote: >>>> >>>>> Possibly relevant: >>>>> https://go.dev/doc/faq#pointer_to_interface >>>>> >>>> -- >>> You received this message because you are subscribed to the Google >>> Groups "golang-nuts" group. >>> To unsubscribe from this group and stop receiving emails from it, send >>> an email to golang-nuts...@googlegroups.com. >>> To view this discussion on the web visit >>> https://groups.google.com/d/msgid/golang-nuts/948dc8f3-385e-4f50-a44b-ddf4792e6362n%40googlegroups.com >>> >>> <https://groups.google.com/d/msgid/golang-nuts/948dc8f3-385e-4f50-a44b-ddf4792e6362n%40googlegroups.com?utm_medium=email&utm_source=footer> >>> . >>> >> -- >> You received this message because you are subscribed to the Google Groups >> "golang-nuts" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to golang-nuts...@googlegroups.com. >> > To view this discussion on the web visit >> https://groups.google.com/d/msgid/golang-nuts/CAEkBMfE7XX%2BnHJTW36GaoLjQWidY7GCjpHP1zyOJJzMcSq13JA%40mail.gmail.com >> >> <https://groups.google.com/d/msgid/golang-nuts/CAEkBMfE7XX%2BnHJTW36GaoLjQWidY7GCjpHP1zyOJJzMcSq13JA%40mail.gmail.com?utm_medium=email&utm_source=footer> >> . >> > > > -- > > > Thanks & Regards > Deeptiman Pattnaik > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/b09312e7-c52e-4517-bed1-1ff0352cabf5n%40googlegroups.com.
Re: [go-nuts] How to cast *interface{} into *T ?
Thank you everyone for your help. The problem still exists. I am looking for a solution where s and s2 point to the same instance. The current example proves they are different instances. https://go.dev/play/p/_JyfJelhIy4 The main goal is to store any type in a wrapper and then get the reference of the actual struct. Also it must be the same instance, so the println should print 2, 2, 2 But look like there is no obvious solution. :-( On Friday, 25 November 2022 at 08:19:17 UTC Brian Candler wrote: > To give a real-world example: > > https://pkg.go.dev/github.com/prometheus/client_golang/prometheus > > type metrics struct { > cpuTempprometheus.Gauge > hdFailures *prometheus.CounterVec > } > > Question: why is prometheus.Gauge not a pointer, but > *prometheus.CounterVec is a pointer? > > Answer: because prometheus.Gauge is an interface, whereas > prometheus.CounterVec is a struct. > > An interface essentially already *contains* a pointer to a data value - in > fact, a tuple of (type, pointer). So you should never take a pointer to an > interface. Just pass the interface value, and it will copy the (type, > pointer) pair. > > However, when you extract a value from an interface, it *will* always copy > the internal value. This avoids aliasing issues: someone who passes an > interface value to someone else, won't expect the recipient to be able to > change the sender's copy. > > e.g. > https://go.dev/play/p/u5y3Kwm9Ydz > > Of course, if your interface *contains* a pointer, then the recipient of > the interface can modify the thing being pointed to. > https://go.dev/play/p/o_XAJtNuyGF > > But they can't modify the pointer itself held within the interface, to > make it point to something else. > > The short version is: avoid pointers to interfaces, as the FAQ says. > Instead, let a concrete pointer value satisfy an interface. > > On Friday, 25 November 2022 at 07:40:59 UTC Nigel Tao wrote: > >> Possibly relevant: >> https://go.dev/doc/faq#pointer_to_interface >> > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/948dc8f3-385e-4f50-a44b-ddf4792e6362n%40googlegroups.com.
Re: [go-nuts] How to cast *interface{} into *T ?
Ok, finally got it: https://go.dev/play/p/QFQOpqWsR6S Thanks everyone, especially Axel On Thursday, 24 November 2022 at 23:14:27 UTC Denis P wrote: > Ok, looks like it doesn't work. > > I have got the following error: > panic: interface conversion: interface {} is MyStruct, not *MyStruct > > So the actual code is this: https://go.dev/play/p/jBiPII7rOxG > > Cannot find out what is going on :-( > > On Thursday, 24 November 2022 at 22:29:21 UTC axel.wa...@googlemail.com > wrote: > >> Hi, >> >> >> On Thu, Nov 24, 2022 at 11:16 PM Denis P wrote: >> >>> Hi guys, tried to look everywhere and cannot find an answer. >>> >>> So literally I have this: >>> ``` >>> var result interface{} = &MyStruct{} >>> >>> ... >>> >>> return result.(*MyStruct) // Error: invalid operation: result >>> (variable of type *interface{}) is not an interface >>> ``` >>> >> >> No, offense, but you literally do not have that code, because the error >> message does not fit that code. In your code, `result` has type >> `interface{}`, whereas in the `return` statement, according to the error >> message, it has type `*interface{}`. Ergo, you are either shadowing >> `result` in-between, or the code is not equivalent. >> >> In any case, the immediate solution could be to dereference the pointer: >> return (*result).(*MyStruct) >> >> >>> >>> Is there any way to get the &MyStruct as a result? >>> >>> -- >>> You received this message because you are subscribed to the Google >>> Groups "golang-nuts" group. >>> To unsubscribe from this group and stop receiving emails from it, send >>> an email to golang-nuts...@googlegroups.com. >>> To view this discussion on the web visit >>> https://groups.google.com/d/msgid/golang-nuts/25da3c8f-6126-439a-904a-5fec59cb09f4n%40googlegroups.com >>> >>> <https://groups.google.com/d/msgid/golang-nuts/25da3c8f-6126-439a-904a-5fec59cb09f4n%40googlegroups.com?utm_medium=email&utm_source=footer> >>> . >>> >> -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/ee00cfa3-cdcb-4756-9cf6-9cfcc9abbf12n%40googlegroups.com.
Re: [go-nuts] How to cast *interface{} into *T ?
Ok, looks like it doesn't work. I have got the following error: panic: interface conversion: interface {} is MyStruct, not *MyStruct So the actual code is this: https://go.dev/play/p/jBiPII7rOxG Cannot find out what is going on :-( On Thursday, 24 November 2022 at 22:29:21 UTC axel.wa...@googlemail.com wrote: > Hi, > > > On Thu, Nov 24, 2022 at 11:16 PM Denis P wrote: > >> Hi guys, tried to look everywhere and cannot find an answer. >> >> So literally I have this: >> ``` >> var result interface{} = &MyStruct{} >> >> ... >> >> return result.(*MyStruct) // Error: invalid operation: result (variable >> of type *interface{}) is not an interface >> ``` >> > > No, offense, but you literally do not have that code, because the error > message does not fit that code. In your code, `result` has type > `interface{}`, whereas in the `return` statement, according to the error > message, it has type `*interface{}`. Ergo, you are either shadowing > `result` in-between, or the code is not equivalent. > > In any case, the immediate solution could be to dereference the pointer: > return (*result).(*MyStruct) > > >> >> Is there any way to get the &MyStruct as a result? >> >> -- >> You received this message because you are subscribed to the Google Groups >> "golang-nuts" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to golang-nuts...@googlegroups.com. >> To view this discussion on the web visit >> https://groups.google.com/d/msgid/golang-nuts/25da3c8f-6126-439a-904a-5fec59cb09f4n%40googlegroups.com >> >> <https://groups.google.com/d/msgid/golang-nuts/25da3c8f-6126-439a-904a-5fec59cb09f4n%40googlegroups.com?utm_medium=email&utm_source=footer> >> . >> > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/d1a148b6-99f1-47fa-9c4a-4c8823fb15acn%40googlegroups.com.
[go-nuts] How to cast *interface{} into *T ?
Hi guys, tried to look everywhere and cannot find an answer. So literally I have this: ``` var result interface{} = &MyStruct{} ... return result.(*MyStruct) // Error: invalid operation: result (variable of type *interface{}) is not an interface ``` Is there any way to get the &MyStruct as a result? -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/25da3c8f-6126-439a-904a-5fec59cb09f4n%40googlegroups.com.
[go-nuts] Method chaining in multiple lines
Hi guys, I am struggling with making my code work in a way that multiple methods called in multiline approach. Does any gopher has an answer is there any solution to this: ``` type MyStuff struct { } func CreateMyStuff() MyStuff {} func (s MyStuff) DoJobOne() MyStuff {} func (s MyStuff) DoJobTwo() MyStuff {} ... result := CreateMyStuff() . DoJobOne() // Error: expected statement, found '.' . DoJobTwo() ``` I have an error " expected statement, found '.' ". Is there any workaround? -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/182bae0c-de3f-417e-82bb-07ca3d26e71an%40googlegroups.com.
[go-nuts] cannot initialize 1 variables with 2 values
Hi guys, Does anyone know how to pass it properly and without adding extra lines of code? ``` func GetAsOr[T any](t interface{}, e error) (T, error) { return t.(T), e } func (s ServiceProvider) GetServiceOr() (interface{}, error) { } result := GetAsOr[MyObj](services.GetServiceOr()) // Error: cannot initialize 1 variables with 2 values ``` -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/308dd860-a980-449d-a710-201f074e4358n%40googlegroups.com.