Loosing type after json.Unmarshal

37 views
Skip to first unread message

marian.s...@gmail.com

unread,
Feb 20, 2018, 1:22:43 PM2/20/18
to golang-nuts
Hi,

please have a look at this code why do I loose type information after json.Unmarshal?

Burak Serdar

unread,
Feb 20, 2018, 1:40:55 PM2/20/18
to marian.s...@gmail.com, golang-nuts
structA is an interface{}. Before unmarshal, it is pointing to a
StructA value. Unmarshal, seeing that the argument is an interface{},
unmarshals the JSON into a map[string]interface{}, and points structA
to it.

If you do:
structA := slice["struct-a"].(StructA)

structA will becoma a StructA type variable, and will do what you expect.


>
> --
> 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.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages