A go:generate directive is a specially formatted Go source comment that tells go generate to execute the command following //go:generate. It keeps repeatable code-generation commands close to the source they serve. section 10.8 section 10.1.2
Generating gRPC files
Place a directive in generate.go when generating Go and gRPC code from .proto files: //go:generate bash -c "protoc -I=api/proto/ {...} api/proto/*.proto". The shell wrapper matters because go generate does not expand *.proto itself. Run go generate to execute the documented command. section 10.1.2
Use the directive to make generated artifacts reproducible and discoverable:
- Put generation commands in a generate.go file, a Makefile, or a script where maintainers can find them. section 10.1.2
- Generate mocks directly from an interface, for example with minimock: //go:generate minimock -i habitCreator -s "_mock.go" -o "mocks". section 10.4.1
- Run go generate . for one package or go generate ./... for all project generation commands. section 10.4.1
Inspect the resulting project tree to verify that generate.go and generated Go files appear in the intended directories. section 10.1.2
