英文:
protoc-gen-gogo always appends a '_' to the field in the message
问题
尝试通过protoc-gen-gogo生成golang的pb.go文件。但是似乎有一个特定的字段uint64 size
总是被生成为Size_
,带有一个意外的下划线。
消息定义如下:
message T {
uint64 size = 1;
}
=>
在pb.go中的定义如下:
type T struct {
Size_ ....
}
因此,我的编辑器总是弹出一个错误,说没有定义Size_。
我生成的命令是:
protoc(v3) --gogo_out=. --gogo_opt=paths=source_relative *.proto
英文:
goTrying to generate golang pb.go file through protoc-gen-gogo. But it seems that there is a specific field 'uint64 sizeis always generated as
Size_` with an unexpected _
The message is
message T {
uint64 size = 1;
}
=>
The definition in the pb.go is
type T struct {
Size_ ....
}
Thus my editor always pops an error like there no definition of Size_
My generated command is
protoc(v3) --gogo_out=. --gogo_opt=paths=source_relative *.proto
答案1
得分: 2
下划线可以添加到字段名称的末尾,以防止与protoc-gen-go生成的名称发生冲突。Size()方法是生成器创建的基本方法之一,用于获取protobuf消息的大小。对于目标语言(在此示例中为Golang)中的保留关键字也适用相同的规则。
英文:
Underscores may be appended to field names that might collide in anyway with generated names by protoc-gen-go. Size() method is one of the essentials method created by the generator to get the size of the protobuf message. The same applies for keywords reversed by target language (Golang in this instance).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论