亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

訪問未定義的數組元素的結構類型時出現錯誤

訪問未定義的數組元素的結構類型時出現錯誤

Go
開心每一天1111 2023-07-31 15:02:53
我是 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)
}


查看完整回答
反對 回復 2023-07-31
  • 1 回答
  • 0 關注
  • 114 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號