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

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

從 go lists 中接收結構的實例

從 go lists 中接收結構的實例

Go
ibeautiful 2022-11-28 14:47:37
我在 go 中有一個結構是:type AcceptMsg struct {    state         protoimpl.MessageState    sizeCache     protoimpl.SizeCache    unknownFields protoimpl.UnknownFields    Rnd  *Round `protobuf:"bytes,1,opt,name=rnd,proto3" json:"rnd,omitempty"`    Slot *Slot  `protobuf:"bytes,2,opt,name=slot,proto3" json:"slot,omitempty"`    Val  *Value `protobuf:"bytes,3,opt,name=val,proto3" json:"val,omitempty"`}我已經將該結構中的實例添加到acceptMsgQueue     *list.List  我的問題中,當我從列表中接收到它們時,如何訪問實例的變量:for f := p.acceptMsgQueue.Front(); f != nil; f = f.Next() {    acceptMsg := f.Value}當我在 vscode 中將點從 of放入時,它無法將其識別為正確的類型,并且acceptMsg我無權訪問Rnd和作為.SlotValacceptMsg
查看完整描述

1 回答

?
慕姐8265434

TA貢獻1813條經驗 獲得超2個贊

文檔中,列表的元素值是使用any(又名interface{})存儲的:

type Element struct {   

    Value any

}

所以要查看您的原始具體類型值,您需要執行類型斷言


acceptMsg, ok := f.Value.(AcceptMsg) // ok==true if dynamic type is correct


查看完整回答
反對 回復 2022-11-28
  • 1 回答
  • 0 關注
  • 105 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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