Strange behaviour of json marshal of embedded struct

https://play.golang.org/p/DD8K9CBKZMI

I really do not like this. I had an ugly bug in my code. What do you mean will be result of json for struct A and B ?

type A struct {
	Name string
	P
	Q
}

type B struct {
	Name string
	P
}

type P struct {
	Age int
}

type Q struct {
	Age int
}
1 Like

It seems json.Marshall doe not support anonymus structures. Please change the definition of your A structure to
type A struct {
Name string
MyP P
MyQ Q
}

and make sthe corresponding change where you refrence those structures :slight_smile:
a.MyP = P{Age: 34}
a.MyQ = Q{Age: 35}

1 Like

https://play.golang.org/p/eof7urTUx19
you need to add json fields

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.