I'm very new to golang and I'd like to make a json object like this:
{
"name" : "animals",
"children" : [
{"name":"dog", "value": 5},
{"name":"cat", "value": 4},
{"name":"fish", "value": 10}
]
}
The code that I came up with:
type Child struct {
Name string
Value int
}
type DataMap struct {
Name string
Children []Child
}
m := DataMap{"animals", [{"cat": 5 }, {"dog": 4}, {"fish":10}] }
cj, _ := json.Marshal(m)
But I get error:
syntax error: unexpected {, expecting expression
Ideally, I'd like to append the Children
slice to the DataMap
in a loop but I don't know how to do so.
Appreciate your hints.