liveBook · Manning

Field number

As explained in gRPC Microservices in Go

A field number is the numeric identifier assigned to a Protocol Buffers message field for its serialized wire representation.

A field number is the unique numeric identifier assigned to a field within a Protocol Buffers message. It appears beside the field’s type and name in a `.proto` definition and identifies that field in the serialized binary message. In a gRPC order request, `user_id = 1` assigns field number 1 to the user ID. section 3.1.1

syntax = “proto3”                #1
message CreateOrderRequest {
  int64 user_id = 1;             #2
  repeated Item items = 2;       #3
  float amount = 3;              #4
}

The `CreateOrderRequest` definition assigns distinct field numbers to `user_id`, `items`, and `amount`. section 3.1.1

Treat an assigned number as part of the message’s compatibility contract. Do not change it when updating a field. When removing a field, reserve its number and, where appropriate, its name so a later definition cannot reuse them. Reusing an old number for a different type can cause data compatibility problems when a client and server run different message definitions. Before adding a field, check whether its proposed number or name is reserved. section 3.1.1 section 3.4.1

Field numbers also affect encoding size. Protocol Buffers sends field metadata alongside the value; numbers from 1 through 15 fit in a one-byte metadata block, while numbers from 16 through 2,047 take two bytes. Assign low numbers to frequently used fields when planning a message. This choice concerns the field identifier, not the size of its value: a larger integer value can require additional data bytes. section 3.1.1 section 3.1.2

How gRPC Microservices in Go covers it

How deep it goes
Explains numbering, reservation, compatibility, and byte-size implications, then traces a simple integer field through binary encoding.
Examples
Uses numbered order-request fields and integer request values to illustrate message definitions and encoding.
What it assumes
Introduces field numbering through message definitions and explains its encoding role with a simple integer field.
What you can do after
Apply it via `.proto` definitions by assigning distinct numbers, reserving removed identifiers, and checking reservations before adding fields.
How it connects
Connects field numbers to Protocol Buffers messages, binary encoding, gRPC, and backward compatibility.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of gRPC Microservices in Go. The section links point to the parts of the book it draws on.