liveBook · Manning

Go generate

As explained in Learn Go with Pocket-Sized Projects

go generate is a Go command that executes source-code generation directives found in comments.

go generate executes source-code generation commands written in comments using the //go:generate directive. It turns generation into a repeatable project operation instead of an undocumented terminal command. section 10.8 section 10.1.2

For a gRPC API, place a directive in a generate.go file and invoke protoc against the .proto files. Keep the command documented so future maintainers can reproduce the generated files. section 10.1.2

Generate gRPC sources

//go:generate bash -c "protoc -I=api/proto/ ... api/proto/*.proto" A shell wrapper matters when the command uses *.proto: go generate does not expand that wildcard itself. A Makefile or script is an alternative when the command is easier to maintain there. section 10.1.2

How Learn Go with Pocket-Sized Projects covers it

How deep it goes
Explains directives, shell wildcard limitations, command placement, and output paths, but stops short of general generator design.
Examples
Uses generate.go, protoc commands, *.proto inputs, source-relative paths, and a generated project tree.
What it assumes
Assumes familiarity with Go projects and introduces directive placement, protoc invocation, generator installation, and shell expansion limits.
What you can do after
Operate/configure repeatable gRPC code generation through a documented //go:generate directive or maintained build command.
How it connects
Connects source directives with protoc, Protocol Buffers, gRPC, Makefiles, shell commands, and generated Go code.

Also explained in

Compare how 2 books explain go generate →

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.