2 回答

TA貢獻1860條經(jīng)驗 獲得超8個贊
您可以將一個結(jié)構(gòu)“嵌入”到另一個結(jié)構(gòu)中:
type Items string
type Data struct {
Name string
Description string
HasMore bool
}
type DataWithItems struct {
Data // Notice that this is just the type name
Items []Items
}
func main() {
d := DataWithItems{}
d.Data.Name = "some-name"
d.Data.Description = "some-description"
d.Data.HasMore = true
d.Items = []Items{"some-item-1", "some-item-2"}
result, err := json.Marshal(d)
if err != nil {
panic(err)
}
println(string(result))
}
這打印
{"Name":"some-name","Description":"some-description","HasMore":true,"Items":["some-item-1","some-item-2"]}
- 2 回答
- 0 關(guān)注
- 131 瀏覽
添加回答
舉報