liveBook · Manning

Protobuf message

As explained in Learn Go with Pocket-Sized Projects

A Protobuf message is a schema-defined structured data type composed of named fields with assigned numeric identifiers.

A Protobuf message is a schema-defined structured data type. Declare it in a .proto file with named, typed fields and unique numeric identifiers that consumers use to recognize fields across compatible versions. section 10.1.1

A practical message declaration follows these rules:

  • Give every field a unique identifier and preserve assigned numbers permanently when maintaining compatibility. section 10.1.1
  • Write each field with its type, name, and numeric identifier; use comments to document how consumers should use it. section 10.1.1
  • Use messages as service request and response payloads, including wrapper messages when an endpoint needs future-compatible changes. section 10.1.1

Habit entity

A habit message can contain a string name with identifier 1 and an int32 weekly_frequency with identifier 2. A CreateHabitRequest can reuse those ideas, while CreateHabitResponse returns a Habit message. section 10.1.1

Listing 10.3 habit.proto: Defining the Habit message
// Habit represents an objective one wants to complete 
// a given number of times per week.
message Habit { #1
    // Name of the habit, cannot be empty
    string name = 1; #2
    // Frequency, expressed in times per week. 
    int32 weekly_frequency = 2;
}

Inspect the complete Habit declaration, including field comments, types, and identifiers. section 10.1.1

How Learn Go with Pocket-Sized Projects covers it

How deep it goes
Explains declaration, field syntax, comments, numeric identity, and compatibility without detailing serialization internals.
Examples
Uses Habit, CreateHabitRequest, and CreateHabitResponse schemas with names and weekly frequencies.
What you can do after
Build it from scratch by declaring typed, numbered Protobuf data structures for generated Go APIs.
How it connects
Connects messages with gRPC services, request and response types, Go code generation, and backward compatibility.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Go with Pocket-Sized Projects. The section links point to the parts of the book it draws on.