用法:
usage: build [-o output] [-i] [build flags] [packages]
Build 会编译哪些通过import导入的包和那些包依赖的包,但是不会install 编译的结果。如果build的参数是一系列以.go结尾的文件, 则build会将它们每一个看做是一个单独的包。当编译一个单独的main包时,build会将结果写入一个可执行的文件,文件名称默认是 main 方法所在的文件名。当编译多个包或一个非main包时,build会编译这些包,但是会丢掉结果对象。该操作仅用来检查这些包是否可以被编译。
-o 选项仅在编译单独的一个包时可用。该选项告诉build将编译的结果强制写入-o选项指定名称的文件中或可执行文件中。
-i 选项告诉build安装哪些依赖的包。
build子命令的选项可以被这些命令共享:build, clean, get, install, list, run, 和 test commands。
- a 强制重新编译需要编译的包。
- n 打印
build命令的执行过程使用的命令但是不执行它们 - -p n 指定能并行执行的
build个数。默认和可用的 CPU 个数相同。 在darwin/arm平台上,默认值为 1。 - race 启用数据种类监测。只在
linux/amd64,freebsd/amd64,darwin/amd64和windows/amd64这些平台上可用。 - v 输出被编译的包的名称。
- work 输出临时工作文件夹的名称并且在 build 结束后不删除该目录。
- x 打印执行的命令
- asmflags ‘flag list’ arguments to pass on each go tool asm invocation.
- buildmode mode 指定使用的编译模式。参考
go help buildmode获取更多信息 - compiler name 指定要使用的编译器 (gccgo or gc)。
- gccgoflags ‘arg list’ 指定传递给 gccgo compiler/linker 的调用参数.
- gcflags ‘arg list’ 指定传递给每个 go 工具的编译参数
- installsuffix suffix 指定安装包文件夹名称的后缀,以此来将编译结果输出到不同的文件夹。如果使用了
-race选项, 则安装目录后缀就是_race。如果同时使用了-race 和 -installsuffix两个选项,则同时使用这个的组合。 a similar - ldflags ‘flag list’ 指定传递给每个 go 链接工具的参数
- linkshared link against shared libraries previously created with -buildmode=shared
- pkgdir dir 指定安装和加载包的目录,用次目录来代替默认的 pkg 目录
- tags ‘tag list’ 提供给 build 命令的编译参数
gcflags 的参数列表
go tool compile -husage: 6g [options] file.go...-% debug non-static initializers-+ compiling runtime-A for bootstrapping, allow 'any' type-B disable bounds checking-D pathset relative path for local imports-E debug symbol export-I directoryadd directory to import search path-K debug missing line numbers-L use full (long) path in error messages-M debug move generation-N 禁用优化-P debug peephole optimizer-R debug register optimizer-S print assembly listing-V print compiler version-W debug parse tree after type checking-asmhdr filewrite assembly header to file-buildid idrecord id as the build id in the export metadata-completecompiling complete package (no C or assembly)-cpuprofile filewrite cpu profile to file-d listprint debug information about items in list-dynlinksupport references to Go symbols defined in other shared libraries-e no limit on number of errors reported-f debug stack frames-g debug code generation-h halt on error-i debug line number stack-importmap definitionadd definition of the form source=actual to import map-installsuffix suffixset pkg directory suffix-j debug runtime-initialized variables-l 禁用内联-largemodelgenerate code that assumes a large memory model-livedebug liveness analysis-m print optimization decisions-memprofile filewrite memory profile to file-memprofilerate rateset runtime.MemProfileRate to rate-nolocalimportsreject local (relative) imports-o filewrite output to file-p pathset expected package import path-packwrite package file instead of object file-r debug generated wrappers-raceenable race detector-s warn about composite literals that can be simplified-sharedgenerate code that can be linked into a shared library-trimpath prefixremove prefix from recorded source file paths-u reject unsafe code-v increase debug verbosity-w debug type checking-wbenable write barrier (default 1)-x debug lexer-y debug declarations in canned imports (with -d)
ldflags 参数列表
go tool link -husage: link [options] main.o-B noteadd an ELF NT_GNU_BUILD_ID note when using ELF-C check Go calls to C code-D addressset data segment address (default -1)-E entryset entry symbol name-H typeset header type-I linkeruse linker as ELF dynamic linker-L directoryadd specified directory to library path-R quantumset address rounding quantum (default -1)-T addressset text segment address (default -1)-V print version and exit-W disassemble input-X definitionadd string value definition of the form importpath.name=value-a disassemble output-buildid idrecord id as Go toolchain build id-buildmode modeset build mode-c dump call graph-cpuprofile filewrite cpu profile to file-d disable dynamic executable-extld linkeruse linker when linking in external mode-extldflags flagspass flags to external linker-f ignore version mismatch-g disable go package data checks-h halt on error-installsuffix suffixset package directory suffix-k symbolset field tracking symbol-linkmode modeset link mode (internal, external, auto)-linksharedlink against installed Go shared libraries-memprofile filewrite memory profile to file-memprofilerate rateset runtime.MemProfileRate to rate-n dump symbol table-o filewrite output to file-r pathset the ELF dynamic linker search path to dir1:dir2:...-raceenable race detector-s disable symbol table-sharedgenerate shared object (implies -linkmode external)-tmpdir directoryuse directory for temporary files-u reject unsafe packages-v print link trace-w disable DWARF generation
https://leokongwq.github.io/2016/10/14/golang-tools-build.html
