-
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: decoding JSON into anonymous struct works in go 1.8.7 but fails silently in go 1.9.4 #23861
Comments
You two both forgot to check for errors. That might not necessarily matter, but it's a distracting bug report, since everybody will immediately assume the answer is in the ignored error. |
I can update the play link and check the error if you like, but there is not one. :) |
Well, we can investigate, but there's no fixing Go 1.9 at this point, and Go 1.10 is out, like, today, so probably not changing there either. I see nothing in the Go 1.9 release notes about json or reflect. Please test new Go releases early & often! :) |
Just an FYI: The same behaviour as go 1.9 is exhibited in go 1.10rc2 |
An exported field works: https://play.golang.org/p/0kdRTJY_Xp5 Capitalizing the type works: https://play.golang.org/p/4szuu5Zydg3 From https://golang.org/pkg/encoding/json/#Marshal:
|
The behavior changed in b817359. |
Hey @dsnet , is that expected? A pointer to a string is considered unexported? |
Yes, this is expected behavior. According to the language spec:
Thus, embedding a builtin type results in a field name that is not exported (since all builtin have lowercase names). Since this is unexported, the The
The example you provided has an anonymous string, not a struct. |
Furthermore, in Go1.10 and on, it wouldn't be possible for The reason why |
Please answer these questions before submitting your issue. Thanks!
What version of Go are you using (
go version
)?1.8.7 and 1.9.4
Does this issue reproduce with the latest release?
yes
What operating system and processor architecture are you using (
go env
)?What did you do?
When decoding json into an anonymous structure, the data is present when using an app built with go version 1.8.7 but empty when using go 1.9.7
If possible, provide a recipe for reproducing the error.
A complete runnable program is good.
A link on play.golang.org is best.
https://play.golang.org/p/vPsyOX7wBea
Here is an example of how I discovered this using the req.Body, details in the README show the difference between the go 1.8 and 1.9 output. With help from eric_hill on IRC #go-nuts we narrowed it down the the above play link.
https://github.com/nkhumphreys/goreqbody
What did you expect to see?
localStr should be populated with the data decoded from the JSON
What did you see instead?
localStr was empty
The text was updated successfully, but these errors were encountered: