~$ go tool addr2line api asm buildid cgo compile cover dist doc fix link nm objdump pack pprof test2json trace vet
查看 go tool 子命令帮助文档:
1 2 3 4 5 6 7 8 9
~$ go doc cmd/compile Compile, typically invoked as “go tool compile,” compiles a single Go package comprising the files named on the command line. It then writes a single object file named for the basename of the first source file with a .o suffix. The object file can then be combined with other objects into a package archive or passed directly to the linker (“go tool link”). If invoked with -pack, the compiler writes an archive directly, bypassing the intermediate object file. ...
1 2 3 4 5
~$ go doc cmd/link Link, typically invoked as “go tool link,” reads the Go archive or object for a package main, along with its dependencies, and combines them into an executable binary. ...
go build -gcflags
提供编译选项,如禁用编译器优化和内联(go1.10 之前的版本):
1
~$ go build -gcflags="-N -l"
go1.10+ 版本:
1
~$ go build -gcflags="all=-N -l"
具体支持的编译选项可以参考 go doc cmd/compile 帮助文档中 Flags 一节。
go build -ldflags
提供链接选项,如为包中的变量赋值:
1
~$ go build -ldflags "-X github.com/runsisi/x-ctl/cmd.Version=$(git describe)"
如果要传递的字符串可能存在空白字符,则需要用引号括起来:
1
~$ go run -ldflags "-X '${GIT_IMPORT}.Version=${GIT_DESCRIBE}${GIT_DIRTY}' ${LD_FLAGS}"
注意其中的单引号是在双引号内部,所以里面的 shell 变量会展开。
具体支持的编译选项可以参考 go doc cmd/link 帮助文档中 Flags 一节。
go list -m
列出项目当前所有的依赖模块:
1 2 3 4 5 6
$ go list -m all github.com/spf13/cobra github.com/BurntSushi/toml v0.3.1 github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6 github.com/coreos/etcd v3.3.10+incompatible ...
列出匹配规则指定的依赖模块:
1 2 3 4 5 6 7
~$ go list -m github.com/spf... github.com/spf13/cobra github.com/spf13/afero v1.1.2 github.com/spf13/cast v1.3.0 github.com/spf13/jwalterweatherman v1.0.0 github.com/spf13/pflag v1.0.3 github.com/spf13/viper v1.3.2
1 2 3
~$ go list -m ...mitche... github.com/mitchellh/go-homedir v1.1.0 github.com/mitchellh/mapstructure v1.1.2