14
package tutorial;

message Person {
  required string name = 1;
  required int32 id = 2;
  optional string email = 3;
}

What does "1", "2", "3" mean ?

why
  • 23,923
  • 29
  • 97
  • 142

2 Answers2

13

Each field in the message definition has a unique numbered tag. These tags are used to identify your fields in the message binary format, and should not be changed once your message type is in use.

https://web.archive.org/web/20120321055631/http://code.google.com/intl/de-DE/apis/protocolbuffers/docs/proto.html

Don Cruickshank
  • 5,641
  • 6
  • 48
  • 48
  • 1
    Can't the field number be inferred from the declaration? – CraigDavid Apr 06 '21 at 04:14
  • 2
    @WilderField With explicit numbering, you can sort and organize the fields in arbitrary order without affecting the generated code. That will be useful when refactoring. – Chang Qian Dec 15 '21 at 11:46
7

They're the field numbers - they're used in the wire representation to identify which field is associated with a value. This means that renaming a field isn't a breaking change (in terms of wire format) and the names themselves don't have to be serialized.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194