首页 > 后端开发 > Golang > 正文

Protobuf消息没有实现protoreflect.ProtoMessage(ProtoReflect方法有指针接收器)

WBOY
发布: 2024-02-09 18:27:20
转载
521 人浏览过

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学习者快速成长!