我有一个Protobuf消息导入"google/protobuf/any.proto"
:
message MintRecord {
...
google.protobuf.Any data = 11;
...
}
我正试图序列化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),
})