Skip to content
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: Unmarshal into *interface{} that contains specific value #6640

gopherbot opened this issue Oct 22, 2013 · 4 comments


Copy link

by Lytvynov.A.V:

What steps will reproduce the problem?
If possible, include a link to a program on
Pass pointer to an interface{} value that contains specific struct, instead of
interface{} value that contains pointer to specific struct.

What is the expected output?
main.B{Key:"", Val:""}
main.B{Key:"one", Val:"1"}

What do you see instead?
main.B{Key:"", Val:""}
map[string]interface {}{"Key":"one", "Val":"1"}

Which compiler are you using (5g, 6g, 8g, gccgo)?

Which operating system are you using?

Which version are you using?  (run 'go version')
go version devel +b4c37131e846 Mon Oct 21 19:01:24 2013 -0400 linux/amd64
also works on release version

Please provide any additional information below.
Obvious solution to the code above would be
But should it *only* work that way?
Copy link

adg commented Oct 22, 2013

Comment 1:

The first version of the unmarshal function is useless. You're just passing in a struct
value. Even if it "worked", the unmarshal function would populate a copy of that value
and you could never retrieve the contents of the JSON blob.

Copy link

Comment 2 by Lytvynov.A.V:

Indeed, but that was just a simplified example demonstrating the behavior.
Here are similar examples:

Copy link

adg commented Oct 22, 2013

Comment 3:

The issue is that the concrete value of the interface{} value is just a value, and is
not addressable, and so the JSON package can't modify it.
Change the line
    a := &A{B: B{}}
    a := &A{B: &B{}}
and you'll see that it works.
Here is a reduced example that demonstrates the difference:

Status changed to WorkingAsIntended.

Copy link

Comment 4 by Lytvynov.A.V:

Ah, so it's about addressability.
That makes it clearer. Thanks!

@golang golang locked and limited conversation to collaborators Jun 25, 2016
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
None yet

No branches or pull requests

2 participants