How to make go map of such structure:
{
"A": [
{
"name": "My name",
"desc": "aaaa",
"sub": [] //empty
},
{
"name": "Loc",
"desc": "bbbb"
"sub": [
{
"name": "xxxxx",
"desc": "aaaa",
},
{
"name": "yyyyy",
"desc": "aaaa",
},
]
},
],
"B": [
{
"name": "My name b",
"desc": "cccc",
"sub": [] //empty
},
{
"name": "tyty",
"desc": "ffff"
"sub": [
{
"name": "rrrrrr",
"desc": "descrition",
}
]
},
]
}
I tried to do that but i am not getting it. How make empty struct/array and how make struct in struct?
type myStruct struct {
Name string `json:"name"`
Desc string `json:"desc"`
// ?? Sub myStruct `json:"sub"`
}
m := map[string]interface{}{
"A": []myStruct{
{"My name", "aaaa", []???? },
{"Loc", "bbbb", ??? },
},
}
Sub *myStruct.Subfield represent zero or onemyStructs? or zero, one or more? If zero or one, then @BurakSerdar suggestion works. If more than one then use a slice.