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: json.Marshal does not error with name conflicts between anonymous structs #8121

gopherbot opened this issue May 29, 2014 · 6 comments


Copy link


My version:
go version go1.2 darwin/amd64

Steps to reproduce:

What happened?
Field name conflicts within anonymous structs result in empty marshal and no error

What should have happened instead?
It should give an error
Copy link

Comment 1:

Labels changed: added repo-main, release-go1.4.

Copy link

Comment 2:

CL mentions this issue.

Copy link

Comment 3:

Can you please try this CL

Copy link

Comment 4:

CL mentions this issue.

Copy link

rsc commented Oct 6, 2014

Comment 5:

Your CL makes reference to V8 and Python. That's not interesting. This is Go. The Go
rule is that if you have two embedded structs with the same field name at the same depth
in a struct type, that field name is not accessible from the top level of the struct
using the . operator.
json.Marshal uses all the fields that are accessible from the top level using the .
operator. If there are JSON-specific tag overrides, it applies the same Go rule to those
instead of the Go field names. But either way it is the Go semantics that are being used
here, not anything defined by JSON or V8 or Python.
To be very concrete, here is an edited version of your program:
package main
import (
func main() {
    type a struct {
        Hello string
    type b struct {
        Hello string
    type c struct {
    herp := c{a{"hello"},b{"world"}}
This doesn't compile. There is no herp.Hello. Therefore json.Marshal does not marshal

Status changed to WorkingAsIntended.

Copy link

Comment 6 by

Don't you think that json.Marshal should return a runtime error similar to the one given
by the compiler in your example?

@rsc rsc added this to the Go1.4 milestone Apr 14, 2015
@rsc rsc removed the release-go1.4 label Apr 14, 2015
@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

4 participants