liveBook · Manning

Protoc-gen-go

As explained in Learn Go with Pocket-Sized Projects

protoc-gen-go is a protoc plugin that generates Go types and related code from Protocol Buffers schemas.

protoc-gen-go is a protoc plugin that turns Protocol Buffers message definitions into Go source files. protoc-gen-go-grpc performs the corresponding gRPC service generation. section 10.1.2

A practical generation workflow:

  • Install protoc through a system package manager, then install both Go plugins with go install. section 10.1.2
  • Pass --go_out and --go_opt=paths=source_relative to generate files beside the source tree rather than recreating the module path. section 10.1.2
  • Compile all .proto files with -I=api/proto/ so service.proto can resolve imports from habit.proto. section 10.1.2
  • Add --go-grpc_out and --go-grpc_opt for service stubs, optionally disabling required unimplemented servers. section 10.1.2

Generate the API

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 section 10.1.2

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

Use the supplied command listings to compare installation, output-path, import-path, gRPC, and go:generate variants. section 10.1.2

How Learn Go with Pocket-Sized Projects covers it

How deep it goes
Explains installation, output paths, imports, multi-file compilation, gRPC options, and repeatable generation, without plugin internals.
Examples
Uses habit.proto and service.proto, shell commands, generated .pb.go files, and a go:generate directive.
What it assumes
Assumes a Go module and .proto schemas; teaches the required protoc and Go plugin installation steps.
What you can do after
Operate protoc to generate organized Go message and gRPC files from one or more schema files.
How it connects
Connects Protocol Buffers, protoc, Go code generation, gRPC, import paths, and source-relative output.

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.