On Fri, Nov 25, 2022 at 12:14 AM Denis P <denis.puj...@gmail.com> 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 :-( > The runtime is telling you - the dynamic type of the interface is not `MyStruct`, it is `*MyStruct`: w := &MyWrapper{Obj: MyStruct{}} Here, you are putting a `MyStruct` into `w.Obj`. s := GetOf[MyStruct](&w.Obj) You are calling `GetOf` with the address of `w.Obj`. That is, you are passing an `*interface{}` to `GetOf[MyStruct]`. That `*interface{}` points at an interface containing a `MyStruct` (as you just put that in). func GetOf[T interface{}](obj *interface{}) *T { return (*obj).(*T) } After instantiation, you are type-asserting the pointee of the passed pointer to `*MyStruct`. But the value in there is a `MyStruct`. So this panics. I don't know how to fix your code, because I do not understand what you are trying to do. One way to make the panic go away is to put a `*MyStruct` into `w.Obj`: w := &MyWrapper{Obj: &MyStruct{}} (note the second `&`). Another would be, to not type-assert on a `*T`, but a `T` and return a pointer pointing at the same value: x := (*obj).(T) return &x You also probably do not want to take a `*interface{}`, but just take the interface directly… So maybe you want this? https://go.dev/play/p/5w-sMtVyTE3 The code isn't particularly functional, so I have no idea what the intended fix is. But the compiler and runtime are telling you, what you are doing wrong, in either case. > > 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 <denis....@gmail.com> 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 > <https://groups.google.com/d/msgid/golang-nuts/d1a148b6-99f1-47fa-9c4a-4c8823fb15acn%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/CAEkBMfE3A7HOMvRmYTmB6xxyCk3yo%3D5LbEkHSfxeTFwNbV8pMw%40mail.gmail.com.