-
Notifications
You must be signed in to change notification settings - Fork 17.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
encoding/json: unexpected behavior when unmarshalling to "boxed" struct #33993
Comments
You need to use a pointer to |
@brenol can you confirm that this suggestion resolves your issue? If so, this issue can be closed. You can also check out https://github.com/golang/go/wiki/Questions for more resources. |
Yes, the suggestion fixes the issue. However, is this the expected behavior? I mean, as I mentioned, removing json.Unmarshal altogether does work and it does not panic. My main issue here is: should json.Unmarshal change the underlying type of a by value struct to map[string]interface{}? I tried looking up for some reason this happens but it didn't make sense to me, as the docs don't make it clear why it works with pointers and not with values. |
/cc @rsc |
Plain structs in interfaces aren't addressable, so they can't be set. |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
https://play.golang.org/p/IOwj_PHRGXK
What did you expect to see?
I did not expect
v
to be of type map[string]interface{}, as its underlying type is clearly Y.What did you see instead?
v
is of type map[string]interface{}, failing the type assertion.Note that if it's a pointer to Y, it works correctly.
Also, this came from a bigger project where Sprintf was reporting both
v
andY{}
with the same underlying type, however, I was unable to reproduce that here.Also, removing json.Unmarshal fixes the issue, as the boxed Y is cast correctly to Y when there is no unmarshal.
The text was updated successfully, but these errors were encountered: