登录
首页 >  Golang >  Go问答

Protobuf消息未正确实现protoreflect.ProtoMessage(ProtoReflect方法需要使用指针接收器)

来源:stackoverflow

时间:2024-02-12 19:00:23 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学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>