liveBook · Manning

Protoc

As explained in Learn Go with Pocket-Sized Projects

protoc is the Protocol Buffers compiler that parses .proto schemas and invokes generators to produce source code.

protoc is the Protocol Buffers compiler: it reads .proto schemas and invokes language-specific plugins to generate source code. In a Go gRPC project, protoc works with protoc-gen-go and protoc-gen-go-grpc to generate message structures, service interfaces, and client/server code. section 10.1.2 section 10.8

A practical Go generation workflow uses these steps:

  • Install protoc with a platform package manager, then install the Go plugins with go install. section 10.1.2
  • Compile a schema with --go_out to select Go output and its destination. section 10.1.2
  • Add --go_opt=paths=source_relative to keep generated files beside the source tree instead of recreating the module path. section 10.1.2
  • Use -I=api/proto/ when schemas import one another, compile all .proto files, and add --go-grpc_out for service code. section 10.1.2
  • Place the command in a //go:generate directive so go generate can repeat it consistently. section 10.1.2 section 10.8

Generate a habits tracker API

From the Go module root, compile habit.proto and service.proto with: protoc -I=api/proto/ --go_out=api/ --go_opt=paths=source_relative --go-grpc_out=api/ --go-grpc_opt=paths=source_relative api/proto/*.proto. The resulting api directory contains habit.pb.go, service.pb.go, and service_grpc.pb.go. section 10.1.2

> apt install -y protobuf-compiler #Linux
> brew install protobuf #Mac

Compare Linux and Mac installation commands for the protoc compiler. section 10.1.2

> protoc -I=api/proto/ --go_out=api/ --go_opt=paths=source_relative 
    --go-grpc_out=api/ --go-grpc_opt=paths=source_relative 
    api/proto/*.proto

Inspect the complete command for generating Go messages and gRPC service code with source-relative paths. section 10.1.2

How Learn Go with Pocket-Sized Projects covers it

How deep it goes
Explains installation, plugins, output paths, imports, multi-file compilation, and repeatable generation commands.
Examples
Uses habit.proto and service.proto, generated Go files, installation commands, and a complete multi-plugin compilation command.
What it assumes
Assumes a Go module and .proto files, while teaching compiler installation, plugin setup, output directives, and import paths.
What you can do after
Build it from scratch by installing protoc, configuring Go plugins, and generating message and gRPC service files.
How it connects
Connects Protocol Buffers with Go, gRPC, code generation, .proto schemas, and go generate.

Also explained in

Compare how 4 books explain protoc →

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.