Protobuf消息未正确实现protoreflect.ProtoMessage(ProtoReflect方法需要使用指针接收器)
时间:2024-02-12
浏览:167
作者:佚名
学习Golang要努力,但是不要急!今天的这篇文章《Protobuf消息未正确实现protoreflect.ProtoMessage(ProtoReflect方法需要使用指针接收器)》将会介绍到等等知识点,如果你想深入学习Golang,可以关注我!我会持续更新相关文章的,希望对大家都能有所帮助!
问题内容
我有一条 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方法需要使用指针接收器)》的详细内容,更多关于的资料请关注golang学习网公众号!
本文内容来源于互联网,如有侵权请联系删除。
近期热门
11.18
11.15
站内热门
最新文章
热门专题
