build flags 使用于以下命令:go build, go clean, go get, go install, go list,go run, go test 如:go build -n -a .
可用参数:
-a
强制重新构建涉及的所有包。
-n
打印将要执行的命令,但不执行
-p n
构建时并行数(n).
默认`GOMAXPROCS`
-race
开启数据竞争检测。
支持的平台: linux/amd64, freebsd/amd64, darwin/amd64, darwin/arm64, windows/amd64,
linux/ppc64le and linux/arm64 (only for48-bit VMA).
-msan
enable interoperation with memory sanitizer.
Supported only on linux/amd64, linux/arm64
and only with Clang/LLVM as the host C compiler.
On linux/arm64, pie build mode will be used.
-asan
enable interoperation with address sanitizer.
Supported only on linux/arm64, linux/amd64.
-v
打印构建的包名称
-work
打印构建临时工作目录并保留
-x
打印构建期间使用的命令
-asmflags '[pattern=]arg list'
构建过程中调用`go tool asm`时使用的参数。参数描述见:`go tool asm`
-buildmode mode
设置构建模式,如插件模式:`-buildmode=plugin`。参数描述见:`go help buildmode`
-buildvcs
是否用版本控制信息标记二进制文件 ("true", "false", 或 "auto"). 默认 ("auto"), 如果main包、main模块包含的目录、当前目录都在同一个存储库(git/mercurial/fossil/bazaar等)中那么版本控制信息会标记到二进制文件中。
`-buildvcs=false` 忽略版本控制信息。
`-buildvcs=true` 版本控制信息可用但因缺少工具或文件结构不明确导致无法将信息标记到二进制文件中将会报错。
另:版本控制信息和构建信息都可以使用`go version -m go编译的二进制文件`或`runtime/debug.ReadBuildInfo()`或`debug/buildinfo.Read()`获取
-compiler name
构建时使用的编译器名称, `runtime.Compiler `(gccgo 或 gc).
-gccgoflags '[pattern=]arg list'
设置gccgo编译器的参数
-gcflags '[pattern=]arg list'
构建过程中调用`go tool compile`时使用的参数。参数描述见:`go tool compile`
-installsuffix suffix
a suffix to use in the name of the package installation directory,
in order to keep output separate from default builds.
If using the -race flag, the install suffix is automatically set to race
or, ifset explicitly, has _race appended to it. Likewise for the -msan
and -asan flags. Using a -buildmode option that requires non-default compile
flags has a similar effect.
-ldflags '[pattern=]arg list'
构建过程中调用`go tool link`时使用的参数。参数描述见:`go tool link`
另:`go build -ldflags="-X 'main.buildTime=`date`'" .`可以将构建时间打入变量`main.buidTime`(`buidTime`必须为string类型,是否可导出无所谓)中。
-linkshared
build code that will be linked against shared libraries previously
created with -buildmode=shared.
-mod mode
模块加载方式:readonly, vendor, 或 mod。
参考:https://go.dev/ref/mod#build-commands
leave newly-created directories in the module cache read-write
instead of making them read-only.
-modfile filein module aware mode, read(and possibly write) an alternate go.mod
file instead of the one in the module root directory. A file named
"go.mod" must still be present in order to determine the module root
directory, but it is not accessed. When -modfile is specified, an
alternate go.sum file is also used: its path is derived from the
-modfile flag by trimming the ".mod" extension and appending ".sum".
-overlay fileread a JSON config file that provides an overlay for build operations.
The file is a JSON struct with a single field, named 'Replace', that
maps each disk file path (a string) to its backing file path, so that
a build will run as if the disk file path exists with the contents
given by the backing file paths, or as if the disk file path does not
exist if its backing file path is empty. Support for the -overlay flag
has some limitations: importantly, cgo files included from outside the
include path must be in the same directory as the Go package they are
included from, and overlays will not appear when binaries and tests are
run through go run and go test respectively.
-pkgdir dirinstall and load all packages from dir instead of the usual locations.
For example, when building with a non-standard configuration,
use -pkgdir to keep generated packages in a separate location.
-tags tag,list
以逗号分隔的tag列表控制编译条件,配合`//go:build tag`使用。参考:https://pkg.go.dev/cmd/go#hdr-Build_constraints
-trimpath
从生成的可执行文件中移除所有的文件系统路径。
记录文件名时使用`module path@version`或普通导入路径。
不会暴露主机的路径信息,这在日志输出代码位置时非常有用。
-toolexec 'cmd args'
a program to use to invoke toolchain programs like vet and asm.
For example, instead of running asm, the go command will run
'cmd args /path/to/asm <arguments for asm>'.
The TOOLEXEC_IMPORTPATH environment variable will be set,
matching 'go list -f {{.ImportPath}}'for the package being built.