← 返回命令列表

Linux command

go-generate 命令

文本

复制后可按需替换文件名、目录或参数。

常用示例

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]

说明

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.

参数

-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.

FAQ

What is the go-generate command used for?

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.

How do I run a basic go-generate example?

Run `go generate` in a terminal, then adjust file names, paths, flags, or remote targets for your system.

What does -run _regexp_ do in go-generate?

Run only directives matching regexp.