Protocol Buffers and .proto Files
Message types, services, and protoc codegen
Protocol Buffers and .proto Files is a free Go Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Go Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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@latestGenerating 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.protoGenerated 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
Frequently asked questions
Is the “Protocol Buffers and .proto Files” lesson free?
Yes — the full text of “Protocol Buffers and .proto Files” is free to read here on the web, and the Go Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Go Academy course, upgrade to CoddyKit PRO.
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; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
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
- Protocol Buffers and .proto Files
- Implementing a Unary gRPC Service
- Server and Client Streaming
- gRPC Interceptors and Metadata