Go Academy · Lesson

Protocol Buffers and .proto Files

Message types, services, and protoc codegen

Lesson 1 of 413 steps

Protocol Buffers and .proto Files is a free Go Academy lesson on CoddyKit. This is lesson 1 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Go Academy learning path, and your progress syncs across the web and the CoddyKit app. The Go Academy course includes 4 lessons in total.

What are Protocol Buffers?

Protocol Buffers (protobuf) is a language-neutral, binary serialization format developed by Google. It is smaller and faster than JSON and is the canonical wire format for gRPC.

.proto file syntax

Define messages and services in a .proto file:

syntax = "proto3";
package user;
option go_package = "github.com/example/user/pb";

message User {
    int64 id   = 1;
    string name = 2;
    string email = 3;
}

service UserService {
    rpc GetUser (GetUserRequest) returns (User);
}
message GetUserRequest { int64 id = 1; }

Field numbers

Each field has a unique number (tag) used in the binary encoding. Never change existing field numbers after the schema is in use — it breaks compatibility.

proto3 defaults

In proto3, all fields have default values (0 for numbers, "" for strings, false for bools). There is no required vs optional distinction — missing fields take the default.

Installing protoc

Install the protobuf compiler and the Go plugin:

brew install protobuf
go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest

Generating Go code

Run protoc to generate Go structs and gRPC stubs:

protoc --go_out=. --go_opt=paths=source_relative \
       --go-grpc_out=. --go-grpc_opt=paths=source_relative \
       user/user.proto

Generated types

protoc generates: a Go struct per message, getter methods, a service interface, an unimplemented server struct, and a client struct with all RPC methods.

Repeated fields

Use repeated to represent a list (maps to Go slice):

message ListUsersResponse {
    repeated User users = 1;
}

Enums

Define enumerations in proto3. The first value must be 0 (the default):

enum Role {
    ROLE_UNSPECIFIED = 0;
    ROLE_USER        = 1;
    ROLE_ADMIN       = 2;
}

Well-known types

The google/protobuf package provides common types: Timestamp, Duration, Struct (JSON-like), Any. Import them in your .proto file.

import "google/protobuf/timestamp.proto";
message Event { google.protobuf.Timestamp created_at = 1; }

Backward compatibility rules

Safe changes: add new fields, add new message types, add new enum values. Unsafe: change field numbers, change field types, remove fields (they silently become the default).

Quick Check

What must be true about field numbers in a protobuf message?

Recap: Protocol Buffers

Key points:

  • .proto defines messages (fields + types) and services (RPCs)
  • Field numbers are permanent — never reuse or change them
  • protoc + go plugin generates Go structs and gRPC stubs
  • repeated fields → slices; enums must start at 0
Free to start

Learn Go with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
51
Lessons
203

Frequently Asked Questions

Is the “Protocol Buffers and .proto Files” lesson free?

Yes — any 3 lessons of the Go Academy learning path, including “Protocol Buffers and .proto Files”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Go Academy course includes 4 lessons in total.

What will I learn in “Protocol Buffers and .proto Files”?

Message types, services, and protoc codegen You practise Go Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Go Academy?

No prior experience is required. Go Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.

How long does the “Protocol Buffers and .proto Files” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Go Academy lesson?

Yes. Every Go Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Protocol Buffers and .proto Files
  2. Implementing a Unary gRPC Service
  3. Server and Client Streaming
  4. gRPC Interceptors and Metadata
← Back to Go Academy