Define a Simple Protobuf Message

Learn how to define a basic protobuf message with scalar fields. Covers syntax declaration, field types, field numbers, and the proto3 default value semantics.

Basic Messages

Detailed Explanation

Anatomy of a Simple Protobuf Message

The most fundamental unit in Protocol Buffers is the message. A message is a structured data type composed of named fields, each with a type and a unique field number. Here is a minimal example:

syntax = "proto3";

message User {
  int32 id = 1;
  string name = 2;
  string email = 3;
  bool is_active = 4;
}

Key Components

Element Purpose
syntax = "proto3" Declares the protobuf version; required as the first non-comment line
message User Defines a new message type named User
int32 id = 1 A 32-bit integer field with field number 1
string name = 2 A UTF-8 string field with field number 2
bool is_active = 4 A boolean field with field number 4

Field Numbers

Every field must have a unique positive integer as its field number. These numbers identify fields in the binary wire format and should not be changed once the message is in use. Numbers 1-15 use one byte in the encoding and are best reserved for frequently used fields.

Proto3 Default Values

In proto3, all fields have implicit default values: 0 for numeric types, false for booleans, and empty string for strings. Fields set to their default value are not serialized on the wire, saving bandwidth.

Use Case

Defining data transfer objects for a REST API or gRPC service where you need a simple, flat structure like a user profile, configuration setting, or event payload.

Try It — Protobuf Definition Parser

Open full tool