作用
构建编译由导入路径命名的包,以及它们的依赖关系,生成可执行文件.
帮助文档查看方式
go help build
使用
go build [-o 输出名] [-i] [编译标记] [包名]
- 如果参数为***.go文件或文件列表,则编译为一个个单独的包。
- 当编译单个main包(文件),则生成可执行文件。
- 当编译单个或多个包非主包时,只构建编译包,但丢弃生成的对象(.a),仅用作检查包可以构建。
- 当编译包时,会自动忽略'_test.go'的测试文件。
参数说明
-o 编译为某个文件
output 指定编译输出的名称,代替默认的包名。
-i 安装
install 安装作为目标的依赖关系的包(用于增量编译提速)。
以下 build 参数可用在 build, clean, get, install, list, run, test
-a
强制重新构建
-n
打印编译时会用到的所有命令,但不真正执行。
-p n
开启并发编译,默认情况下该值为 CPU 逻辑核数。
-race
开启竞态检测,只支持 linux/amd64, freebsd/amd64, darwin/amd64 和 windows/amd64.
-msan
启用与内存消毒器的互操作。仅支持linux / amd64,并且只用Clang / LLVM作为主机C编译器(少用)。
-v
打印出被编译的包名.
-work
打印临时工作目录的名称,并在退出时不删除它(少用)。
-x
打印编译时会用到的所有命令(-n).
-asmflags 'flag list'
传递每个go工具asm调用的参数(少用)
-buildmode mode
编译模式 详细请查看'go help buildmode'
-compiler name
使用到的编译器的名字, 例如runtime.Compiler (gccgo or gc).
-gccgoflags 'arg list'
传递给每个gccgo编译器/链接器调用的参数。
-gcflags 'arg list'
垃圾回收参数.
-B 禁用边界检查
-N 禁用优化
-l 禁用函数内联
-u 禁用unsafe代码
-m 输出优化信息,表示进行内存分配分析
-S 输出汇编代码
禁止优化和内联可以让运行时(runtime)中的函数变得更容易调试.
-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, if set explicitly, has _race appended to it. Likewise for the -msan
flag. Using a -buildmode option that requires non-default compile flags
has a similar effect.
-ldflags 'flag list'
'-s -w': 压缩编译后的体积
-s 去掉符号表
-w 去掉调试信息,不能gdb调试了
-X 修改字符串符号值 -X main.VER ‘0.99’ -X main.S ‘abc’
-H 链接文件类型,其中包括windowsgui. cmd/ld/doc.go
-linkshared
链接到以前使用创建的共享库
-buildmode=shared.
-pkgdir dir
从指定位置,而不是通常的位置安装和加载所有软件包。例如,当使用非标准配置构建时,使用-pkgdir将生成的包保留在单独的位置。
-tags 'tag list'
构建出带tag的版本.
-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>'.
更多参数
go tool compile -h
go tool link -h