protoc-gen-gogo在消息中的字段后始终添加一个’_’。

huangapple go评论86阅读模式
英文:

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 asSize_` 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).

huangapple
  • 本文由 发表于 2022年3月15日 17:48:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/71480092.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定