I am trying to use the Marshal function to create JSON from a Go struct. The JSON created does not contain the Person struct.
What am I missing?
http://play.golang.org/p/ASVYwDM7Fz
type Person struct {
fn string
ln string
}
type ColorGroup struct {
ID int
Name string
Colors []string
P Person
}
per := Person{
fn: "John",
ln: "Doe",
}
group := ColorGroup{
ID: 1,
Name: "Reds",
Colors: []string{"Crimson", "Red", "Ruby", "Maroon"},
P: per,
}
b, err := json.Marshal(group)
if err != nil {
fmt.Println("error:", err)
}
os.Stdout.Write(b)
The output generated is as follows:
{"ID":1,"Name":"Reds","Colors":["Crimson","Red","Ruby","Maroon"],"P":{}}
I don't see Person in the output.
http://golang.org/pkg/encoding/json/#Marshal