我是 golang 新手,我有一個如下所示的數據結構? ? ?type ParentIDInfo struct {? ? PCOrderID? ? ? string? ? ? ? ?`json:"PCorderId",omitempty"`? ? TableVarieties TableVarietyDC `json:"tableVariety",omitempty"`? ? ProduceID? ? ? string? ? ? ? ?`json:"PRID",omitempty"`}type PCDCOrderAsset struct {? ? PcID? ? ? ? ?string? ? ? ? ? ? ? `json:"PCID",omitempty"`? ? DcID? ? ? ? ?string? ? ? ? ? ? ? `json:"DCID",omitempty"`? ? RequiredDate string? ? ? ? ? ? ? `json:"requiredDate",omitempty"`? ? Qty? ? ? ? ? uint64? ? ? ? ? ? ? `json:"QTY",omitempty"`? ? OrderID? ? ? string? ? ? ? ? ? ? `json:"ORDERID",omitempty"`? ? Status? ? ? ?string? ? ? ? ? ? ? `json:"STATUS",omitempty"`? ? Produce? ? ? string? ? ? ? ? ? ? `json:"Produce",omitempty"`? ? Variety? ? ? string? ? ? ? ? ? ? `json:"VARIETY",omitempty"`? ? Transports? ?[]TransportaionPCDC `json:"Transportaion",omitempty"`? ? ParentInfo? ?[]ParentIDInfo? ? ? ? `json:"ParentInfo",omitempty"`所以我在訪問[]ParentIDInfo內的PCOrderID時遇到問題。我在下面嘗試過,但收到錯誤“pcdcorder.ParentInfo.PCOrderID undefined (type []ParentIDInfo has no field or method PCOrderID)”keyfarmercas = append(keyfarmercas, pcdcorder.ParentInfo.PCOrderID)任何幫助都會非常好
1 回答

瀟瀟雨雨
TA貢獻1833條經驗 獲得超4個贊
PCDCOrderAsset.ParentInfo
不是結構體,它沒有字段PCOrderID
。它是一個切片(元素類型為ParentIDInfo
),因此它的元素也是如此,例如pcdcorder.ParentInfo[0].PCOrderID
。
這是否是你想要的我們無法判斷。pcdcorder.ParentInfo[0].PCOrderID
給出PCOrderID
切片第一個元素的字段。根據您的問題,這可能是也可能不是您想要的。您可能想要附加所有 ID(每個元素一個)。另請注意,如果切片為空(其長度為 0),則會pcdcorder.ParentInfo[0]
導致運行時恐慌。您可以通過首先檢查其長度并僅在其不為空時對其進行索引來避免這種情況。
如果您想添加所有元素的 id,您可以使用循環for
來執行此操作,例如:
for i := range pcdorder.ParentInfo { keyfarmercas = append(keyfarmercas, pcdcorder.ParentInfo[i].PCOrderID) }
- 1 回答
- 0 關注
- 114 瀏覽
添加回答
舉報
0/150
提交
取消