go-generate

run code generation directives in Go source files

TLDR

Run generators in current package
$ go generate
Run generators in all packages
$ go generate ./...
Run specific generator by pattern
$ go generate -run [pattern]
Preview commands without executing
$ go generate -n
Run with verbose output
$ go generate -v
Skip generators matching pattern
$ go generate -skip [pattern]

SYNOPSIS

go generate [-run regexp] [-skip regexp] [-n] [-v] [-x] [packages]

DESCRIPTION

go generate scans Go source files for special directives of the form //go:generate command and executes the specified command. It automates code generation before compilation, commonly used for creating string methods, mock implementations, protocol buffers, and other generated code. Directives must start at the beginning of a line with no space between // and go:generate.

PARAMETERS

-run regexp

Run only directives matching regexp.
-skip regexp
Skip directives matching regexp.
-n
Print commands without executing.
-v
Print package names as processed.
-x
Print commands as executed.

INSTALL

sudo apt install gccgo-go
sudo dnf install gcc-go
sudo pacman -S gcc-go
sudo apk add gcc-go
sudo zypper install gcc-go
brew install go
nix profile install nixpkgs#go

CAVEATS

Environment variables $GOARCH, $GOOS, $GOFILE, $GOLINE, $GOPACKAGE, $GOROOT, $DOLLAR, and $PATH are set during execution. Generated files should include a comment matching **^// Code generated .* DO NOT EDIT\.$ before non-comment content. go generate is never run automatically by go build or go test**; it must be invoked explicitly.

SEE ALSO

go(1), go-build(1)

RESOURCES

Documentation · Source code