[英]some strange fields was added after converting protobuf message to go struct?
我想要一個 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 表轉換為 protobuf 消息?
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.proto
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.