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

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

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

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

Go
胡說叔叔 2022-05-23 15:42:31
我在生成的 proto 文件中有一個結構,如下所示(簡化):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 回答

?
大話西游666

TA貢獻1817條經驗 獲得超14個贊

看起來你正在創建一個切片[]*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')},

        },

    },

}

@mkopriva 上面的評論為此添加了一些很好的解釋。


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

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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