首頁 > 後端開發 > Golang > 主體

Protobuf訊息沒有實作protoreflect.ProtoMessage(ProtoReflect方法有指標接收器)

WBOY
發布: 2024-02-09 18:27:20
轉載
520 人瀏覽過

Protobuf訊息沒有實作protoreflect.ProtoMessage(ProtoReflect方法有指標接收器)

php小編新一在這裡為大家介紹一個問題:在Protobuf訊息中,我們發現沒有實作protoreflect.ProtoMessage介面的ProtoReflect方法,而該方法使用了指標接收器。這個問題可能會導致一些困惑和不便。在文章中,我們將深入探討這個問題的原因和可能的解決方案,以幫助大家更好地理解和應對這個問題。讓我們一起來了解吧!

問題內容

我有一條 protobuf 訊息導入 "google/protobuf/any.proto":

message mintrecord {
    ...
    google.protobuf.any data = 11;
    ...
}
登入後複製

我正在嘗試使用anypb序列化data字段內的另一個protobuf:

data, err := anypb.new(protobuf.lootcrateprize{
    items: &protobuf.inventory{items: items},
    roll:  fmt.sprintf("%f", roll),
})
if err != nil {
    log.println("[lootbox] err: error marshalling lootcrate prize data into mintrec", err)
} else {
    mintrecordproto.data = data
}
登入後複製

編譯後出現以下錯誤:

cannot use protobuf.lootcrateprize{…} (value of type protobuf.lootcrateprize) as type protoreflect.protomessage in argument to anypb.new:
    protobuf.lootcrateprize does not implement protoreflect.protomessage (protoreflect method has pointer receiver)
登入後複製

根據文檔,我在這裡沒有做任何異常的事情。我該如何解決這個問題?

這是我嘗試序列化並儲存在 data 欄位內的 protobuf: lootcrate.proto:

syntax = "proto3";

package protobuf;
option go_package = "protobuf/";

import "protobuf/inventory.proto";
import "protobuf/flowerdbservice.proto";

message LootcratePrize {
    Inventory items = 1;
    repeated NFT flowers = 2;
    string roll = 3;
}
登入後複製

解決方法

sarath sadasivan pillai 是正確的。
將您的程式碼更改為:

data, err := anypb.New(&protobuf.LootcratePrize{
    Items: &protobuf.Inventory{Items: items},
    Roll:  fmt.Sprintf("%f", roll),
})
登入後複製

以上是Protobuf訊息沒有實作protoreflect.ProtoMessage(ProtoReflect方法有指標接收器)的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:stackoverflow.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!