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

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

Protobuf/Go 無法在數組或切片文字中使用文字 * (type *) 作為類型 *

Protobuf/Go 無法在數組或切片文字中使用文字 * (type *) 作為類型 *

Go
富國滬深 2023-08-21 14:22:43
我生成的原型文件中有一個結構,如下所示(簡化):type Record struct {    Field1           int64  `protobuf:"varint,1,opt,name=field1,proto3" json:"field1,omitempty"`    Field2           []byte `protobuf:"bytes,2,opt,name=field2,proto3" json:"field2,omitempty"`}我試圖在我的 Go 文件中調用它func foo(c messagepb.MessageServiceClient){    fmt.Println("Starting to send message...")    msgs := []*messagepb.MessageRequest{        recordpb.Record{ //error msg here            Field1:             1,            Field2:             []byte{byte('a')},        }    }    ...}但我在 recordpb.Record 行收到此錯誤:cannot use recordpb.Record literal (type recordpb.Record) as type *messagepb.MessageRequest in array or slice literal如果有幫助,這是我的messagepb:message.protomessage MessageRequest { recordpb.Record records = 1; }message.pb.gotype MessageRequest struct {    Record               *recordpb.Record `protobuf:"bytes,1,opt,name=record,proto3" json:"record,omitempty"`}我找不到任何有用的信息來解釋為什么會發生這種情況......有什么想法嗎?
查看完整描述

1 回答

?
阿波羅的戰車

TA貢獻1862條經驗 獲得超6個贊

看起來您正在創建一個切片[]*messagepb.MessageRequest,然后recordpb.Record向其中添加一個。recordpb.Record與 不是同一類型*messagepb.MessageRequest。


鑒于這種類型


type MessageRequest struct {

? ? Record *recordpb.Record `protobuf:"bytes,1,opt,name=record,proto3" json:"record,omitempty"`

}

看起來你的msgsvar 應該是


msgs := []*messagepb.MessageRequest{

? ? {

? ? ? ? Record: &recordpb.Record{

? ? ? ? ? ? Field1: 1,

? ? ? ? ? ? Field2: []byte{byte('a')},

? ? ? ? },

? ? },

}


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

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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