我想有一个API来拯救一个用户。这是我的用户信息:
message User {
int32 uid = 1;
string username = 2;
string password = 3;
}我使用protoc-gen-go将其转换为go struct。我发现添加了一些奇怪的字段(XXX_NoUnkeyedLiteral,XXX_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表转换为协议消息?
发布于 2020-03-03 13:41:13
gogoprotobuf是golang/protobuf的一个分支,具有额外的代码生成功能。
protoc gen-gogofaster(没有XXX_unrecognized,指针字段更少)
go get github.com/gogo/protobuf/protoc-gen-gofaster
go install github.com/gogo/protobuf/protoc-gen-gofaster
protoc --gofaster_out=plugins=grpc:. my.protohttps://stackoverflow.com/questions/51591135
复制相似问题