从 go lists 中接收结构的实例

我在 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


ibeautiful
浏览 115回答 1
1回答

慕姐8265434

从文档中,列表的元素值是使用any(又名interface{})存储的:type Element struct {       Value any}所以要查看您的原始具体类型值,您需要执行类型断言:acceptMsg, ok := f.Value.(AcceptMsg) // ok==true if dynamic type is correct
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go