1

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
No9527
  • 31
  • 9

1 Answers1

2

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

Tomas P.
  • 161
  • 2
  • 10
  • Ok, so protoc-gen-go is find to generate Size field in go while not in protoc-gen-go... Is there any way to avoid this underscore when using gogo? Or I can only rename the size to some other name? – No9527 Mar 16 '22 at 06:32
  • I am not aware of any other way other then renaming the field to something else. I usually end up choosing different name of stick with the underscored one. – Tomas P. Mar 16 '22 at 13:24
  • Ok. I guess yours is the only way. Thanks for the answer. – No9527 Mar 17 '22 at 02:42