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



