go generate
go generate scans source files for //go:generate comments and runs the commands written there. It is a convention for code generation, not a build step the compiler runs on its own.
Detailed Explanation
You need a stringer for an error-code type, or a mock for an interface, or a copy of a protobuf. The Go way to record "how this file is produced" is a comment:
//go:generate stringer -type=Level
go generate ./... walks packages, finds those comments, and executes the rest of the line in the file's directory with $GOFILE, $GOPACKAGE, and $GOLINE set. The compiler ignores the comment. go build does not run generate. CI must call go generate (or check that generated files are committed and up to date) if you rely on it.
The command is a shell-less exec. The first token is the program. The rest are arguments. There is no pipe, no &&, and no glob expansion. If you need a shell, the program you run is sh or bash with -c. Prefer a real generator binary on PATH (stringer, mockgen, protoc-gen-go) so Windows and Unix agree.
Generated files are source. Commit them when the generator is not guaranteed to exist on every machine, or when you want reviews to see the output. Put // Code generated ... DO NOT EDIT. at the top so go generate users and linters know the file is mechanical. Do not edit it by hand; edit the comment and rerun.
Scope. go generate runs comments in .go files of the packages you list. It does not understand module graphs beyond those packages. A comment in doc.go is a common place to keep package-wide commands. Order is file name, then line number. Do not depend on a subtle order between packages; generate the dependency first, or use one command that does both.
What it costs. Generate is extra latency in CI if you run it every time. The benefit is a reproducible recipe next to the type it describes. The failure mode is a comment that calls a binary nobody installed, or generated output that drifted from the comment because someone edited only one of them.
Code Examples
package levels
//go:generate stringer -type=Level
type Level int
const (
Info Level = iota
Warn
Error
)# From the module root: run every generate comment.
go generate ./...
# One package only.
go generate ./internal/levelsgo:generate mockgen -source=store.go -destination=store_mock_test.go -package=store
//go:generate mockgen -source=store.go -destination=store_mock_test.go -package=storeCommon Gotchas
`go build` does not run generate
a clean checkout without committed output will not compile if you forgot to run it. Either commit the files or run generate in the build.
The line is not a shell script
quotes are not what you think, and `foo | bar` will try to exec a program named `foo`. Use a small `//go:generate go run ./gen` helper when the command is complex.
Generators should be versioned
`go run` a module or pin a tool in a tools file so `stringer` is not "whatever is on the laptop."
Comments must start at the beginning of the line
`//go:generate` with a space after `//` is the form. A normal comment that mentions go generate does nothing.