用法:

  1. 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/amd64windows/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 的参数列表

  1. go tool compile -h
  2. usage: 6g [options] file.go...
  3. -% debug non-static initializers
  4. -+ compiling runtime
  5. -A for bootstrapping, allow 'any' type
  6. -B disable bounds checking
  7. -D path
  8. set relative path for local imports
  9. -E debug symbol export
  10. -I directory
  11. add directory to import search path
  12. -K debug missing line numbers
  13. -L use full (long) path in error messages
  14. -M debug move generation
  15. -N 禁用优化
  16. -P debug peephole optimizer
  17. -R debug register optimizer
  18. -S print assembly listing
  19. -V print compiler version
  20. -W debug parse tree after type checking
  21. -asmhdr file
  22. write assembly header to file
  23. -buildid id
  24. record id as the build id in the export metadata
  25. -complete
  26. compiling complete package (no C or assembly)
  27. -cpuprofile file
  28. write cpu profile to file
  29. -d list
  30. print debug information about items in list
  31. -dynlink
  32. support references to Go symbols defined in other shared libraries
  33. -e no limit on number of errors reported
  34. -f debug stack frames
  35. -g debug code generation
  36. -h halt on error
  37. -i debug line number stack
  38. -importmap definition
  39. add definition of the form source=actual to import map
  40. -installsuffix suffix
  41. set pkg directory suffix
  42. -j debug runtime-initialized variables
  43. -l 禁用内联
  44. -largemodel
  45. generate code that assumes a large memory model
  46. -live
  47. debug liveness analysis
  48. -m print optimization decisions
  49. -memprofile file
  50. write memory profile to file
  51. -memprofilerate rate
  52. set runtime.MemProfileRate to rate
  53. -nolocalimports
  54. reject local (relative) imports
  55. -o file
  56. write output to file
  57. -p path
  58. set expected package import path
  59. -pack
  60. write package file instead of object file
  61. -r debug generated wrappers
  62. -race
  63. enable race detector
  64. -s warn about composite literals that can be simplified
  65. -shared
  66. generate code that can be linked into a shared library
  67. -trimpath prefix
  68. remove prefix from recorded source file paths
  69. -u reject unsafe code
  70. -v increase debug verbosity
  71. -w debug type checking
  72. -wb
  73. enable write barrier (default 1)
  74. -x debug lexer
  75. -y debug declarations in canned imports (with -d)

ldflags 参数列表

  1. go tool link -h
  2. usage: link [options] main.o
  3. -B note
  4. add an ELF NT_GNU_BUILD_ID note when using ELF
  5. -C check Go calls to C code
  6. -D address
  7. set data segment address (default -1)
  8. -E entry
  9. set entry symbol name
  10. -H type
  11. set header type
  12. -I linker
  13. use linker as ELF dynamic linker
  14. -L directory
  15. add specified directory to library path
  16. -R quantum
  17. set address rounding quantum (default -1)
  18. -T address
  19. set text segment address (default -1)
  20. -V print version and exit
  21. -W disassemble input
  22. -X definition
  23. add string value definition of the form importpath.name=value
  24. -a disassemble output
  25. -buildid id
  26. record id as Go toolchain build id
  27. -buildmode mode
  28. set build mode
  29. -c dump call graph
  30. -cpuprofile file
  31. write cpu profile to file
  32. -d disable dynamic executable
  33. -extld linker
  34. use linker when linking in external mode
  35. -extldflags flags
  36. pass flags to external linker
  37. -f ignore version mismatch
  38. -g disable go package data checks
  39. -h halt on error
  40. -installsuffix suffix
  41. set package directory suffix
  42. -k symbol
  43. set field tracking symbol
  44. -linkmode mode
  45. set link mode (internal, external, auto)
  46. -linkshared
  47. link against installed Go shared libraries
  48. -memprofile file
  49. write memory profile to file
  50. -memprofilerate rate
  51. set runtime.MemProfileRate to rate
  52. -n dump symbol table
  53. -o file
  54. write output to file
  55. -r path
  56. set the ELF dynamic linker search path to dir1:dir2:...
  57. -race
  58. enable race detector
  59. -s disable symbol table
  60. -shared
  61. generate shared object (implies -linkmode external)
  62. -tmpdir directory
  63. use directory for temporary files
  64. -u reject unsafe packages
  65. -v print link trace
  66. -w disable DWARF generation

https://leokongwq.github.io/2016/10/14/golang-tools-build.html