从 go lists 中接收结构的实例

Receiving an instance of a struct from 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
}

当我将 acceptMsg 中的点放入 vscode 中时,它无法将其识别为正确的类型,并且我无权访问 RndSlotVal 作为 acceptMsg.

的属性

docs 列表的元素值存储使用 any (a.k.a。interface{}):

type Element struct {   
    Value any
}

所以要查看您的原始 concrete-typed 值,您需要执行 type assertion:

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