go - 将protobuf消息转换为struct后添加了一些奇怪的字段?

标签 go protocol-buffers

我想要一个API来保存用户。这是我的用户消息:

message User {
    int32 uid = 1;
    string username = 2;
    string password = 3;
}

我使用protoc-gen-go将其转换为struct。我发现添加了一些奇怪的字段(XXX_NoUnkeyedLiteralXXX_unrecognized)。
type User struct {
    Uid                  int32    `protobuf:"varint,1,opt,name=uid,proto3" json:"uid,omitempty"`
    Username             string   `protobuf:"bytes,2,opt,name=username,proto3" json:"username,omitempty"`
    Password             string   `protobuf:"bytes,3,opt,name=password,proto3" json:"password,omitempty"`
    XXX_NoUnkeyedLiteral struct{} `json:"-"`
    XXX_unrecognized     []byte   `json:"-"`
    XXX_sizecache        int32    `json:"-"`
}

由于这些字段,我无法将其保存到MySQL,因为我的MySQL表没有这些字段。有人好的建议吗?我必须创建另一个结构以将MySQL表转换为protobuf消息吗?

最佳答案

gogoprotobuf是golang / protobuf的分支,具有额外的代码生成功能。

protoc-gen-gogofaster(无XXX_无法识别,指针字段较少)

go get github.com/gogo/protobuf/protoc-gen-gofaster

go install github.com/gogo/protobuf/protoc-gen-gofaster

protoc --gofaster_out=plugins=grpc:. my.proto

关于go - 将protobuf消息转换为struct后添加了一些奇怪的字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51591135/

相关文章:

go - 页面完全加载后如何重定向到另一个页面

go - 我的Visual Studio代码应用程序不断删除导入的程序包

java - Gradle 禁用所有增量编译和并行构建

c++ - 从 Google Protocol buffer 中的重复字符串(列表)中删除一个随机值

node.js - 尝试使用 Protocol Buffers - Google 的数据交换格式时,goog 未定义错误

go - 在 GO lang 的 defer 函数中获取 panic() 参数

json - 在 golang 中使用反射将 json 转换为结构

go - 通过xerrors.Errorf()输出错误时,是否可以设置xerrors.Caller(1)?

java - 使用反射 API 填充 Proto 中的 map 字段

c++ - 谷歌 Protocol Buffer 是否支持序列化前的大小计算?