if err := json.Unmarshal([]byte(`{"Foo": 42}`), &v); err !=nil {
panic(err)
}
fmt.Printf("%v%T\n", v, v)
}
funcmain() {
var val S
// map[Foo:42] map[string]interface {}
update(val)
// &{42} *main.S
update(&val)
}
Why would calling by value change the type of the value passed to map? I would expect an interface with a dynamic type of main.S, but its map[string]interface{}, or json.Unmarshal makes it so.
Insights appreciated :)
burak serdar
unread,
Apr 24, 2024, 12:51:21 PM4/24/24
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to cpu...@gmail.com, golang-nuts
In the first case, the interface contains a value of type S, which is
not writable. The value contained in the interface is not addressable.
So Unmarshal creates a new map and fills that. In the second case the
interface contains *S, which is writable, so unmarshal fills it in via
reflection.