On Fri, Jan 12, 2018 at 6:32 PM, Peter Mogensen <a...@one.com> wrote:

> Hi,
>
> I somehow ended up with an interface{} value being a **SomeStruct{}
>
> ... and I need to dereference it to an interface{} holding *SomeStruct{}
> in a generic way (ie. without knowing about the type SomeStruct)
>
> I'm not at all sure this is good idea or the right way for the specific
> code, but out of curiosity, - can this be done in a non-unsafe way?
>

Yes, using reflection. Not using non-reflection, which is WAI. You should
always
* Specialize on specific types, thus knowing them
* Not care what's in the interface
* Reflect to write code that can handle ~anything (e.g. in encoding/json
and the like)


> One would think the result is rather well defined.
>

It's not. The value in the interface doesn't have to be a pointer, so this
is, in general, not defined at all.


>
> In other words,
>
> var x int = 7
> xp := &x
> xpp := &xp
>
> var i1 interface{} = xpp
>
> var i2 interface{}
>
> i2 := SomeMagicNotSpecificToInt(i1)
>
> yp := i2.(*int)
>
> y := *yp  // y now == 7
>
> /Peter
>
> --
> 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.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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.
For more options, visit https://groups.google.com/d/optout.

Reply via email to