为了账号安全,请及时绑定邮箱和手机立即绑定

如何在 Golang 中访问此结构的字段

如何在 Golang 中访问此结构的字段

Go
慕哥9229398 2022-11-28 16:55:35
我是 Golang 的新手,我需要知道如何从格式的结构中访问值:type CurrentSkuList struct {    SubscriptionNumber   string `json:"subscriptionNumber`    Quantity             int    `json:"quantity"`    SubscriptionProducts []struct {        ID              int    `json:"id"`        ActiveStartDate int    `json:"activeStartDate"`        ActiveEndDate   int    `json:"activeEndDate"`        Status          string `json:"status"`        Sku             string `json:"sku"`        ChildIDs        []int  `json:"childrenIds"`    } `json:"subscriptionProducts"`}例如,如果我有一个currentSkus类型的变量CurrentSkuList并且我只需要访问Sku和Status值,有没有办法做类似的事情:currentSkus.SubscriptionProducts.Sku?编辑!当我尝试访问时currentSkus.Quantity出现编译器错误undefined (type []util.CurrentSkuList has no field or method Quantity)。
查看完整描述

1 回答

?
紫衣仙女

TA贡献1839条经验 获得超15个赞

是的,有办法。您可以通过.您建议的语法访问。在这种情况下,CurrentSkuList 返回 SubscriptionProduct 的一个片段,你知道这是因为这[] struct部分。然后你将不得不以这种方式访问数据:

currentSkus.SubscriptionProducts[index].Sku


查看完整回答
反对 回复 2022-11-28
  • 1 回答
  • 0 关注
  • 130 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号