gpt4 book ai didi

arrays - golang中另一个结构内的结构数组

转载 作者:数据小太阳 更新时间:2023-10-29 03:38:34 24 4
gpt4 key购买 nike

<分区>

我是 golang 的新手,正在尝试使用 golang 创建这种格式的 json

{
"Title": "You are awesome",
"Url": "www.youareawesome.com",
"Desc": "your awesome desc is here",
"Payment": {
"Discount": "15%",
"outlets": [
{
"Location": "nowhere"
},
{
"Location": "everywhere"
}
]
}
}

下面是我的struct

代码
type Partner struct {
Title string `json:"Title"`
URL string `json:"Url"`
Desc string `json:"Desc"`
Payment Payment `json:"Payment"`
}

type Payment struct {
Discount string `json:"Discount"`
outletList [] OutletItem `json:"outletList"`
}

type OutletItem struct {
Location string `json:"Location"`
}

我就是这样

partner := Partner{} 
payment := Payment{}
partner.Title = "You are awesome"
partner.Desc = "your awesome desc is here"
payment.Discount = "15%"
payment.AddOutletItem(OutletItem{Location:"nowhere"})
partner.Payment = payment
partner.Url = "www.youareawesome.com"
json.NewEncoder(w).Encode(partner)


func (payment *Payment) AddOutletItem(item OutletItem) []OutletItem {
payment.outletList = append(payment.outletList, item)
return payment.outletList
}

我无法在支付对象中看到 socket 数组,我不确定我是否遗漏了任何东西。

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com