go 命令

$ go build -x

-x会列出来go build调用到的全部命令。html

若是你对Go的工具链好奇,或者使用了一个跨C编译器,而且想知道调用外部编译器用到的具体参数,或者怀疑连接器有bug;使用-x来查看全部调用。android

$ go build -x
WORK=/var/folders/00/1b8h8000h01000cxqpysvccm005d21/T/go-build600909754
mkdir -p $WORK/hello/perf/_obj/
mkdir -p $WORK/hello/perf/_obj/exe/
cd /Users/jbd/src/hello/perf
/Users/jbd/go/pkg/tool/darwin_amd64/compile -o $WORK/hello/perf.a -trimpath $WORK -p main -complete -buildid bbf8e880e7dd4114f42a7f57717f9ea5cc1dd18d -D _/Users/jbd/src/hello/perf -I $WORK -pack ./perf.go
cd .
/Users/jbd/go/pkg/tool/darwin_amd64/link -o $WORK/hello/perf/_obj/exe/a.out -L $WORK -extld=clang -buildmode=exe -buildid=bbf8e880e7dd4114f42a7f57717f9ea5cc1dd18d $WORK/hello/perf.a
mv $WORK/hello/perf/_obj/exe/a.out perf

$ go build -gcflags

这个参数将会传递给编译器。go tool compile -help列出来了全部咱们能够传递给编译器的参数。git

$ go tool compile -help
usage: compile [options] file.go...
  -%    debug non-static initializers
  -+    compiling runtime
  -B    disable bounds checking
  -C    disable printing of columns in error messages
  -D path
        set relative path for local imports
  -E    debug symbol export
  -I directory
        add directory to import search path
  -K    debug missing line numbers
  -L    show full file names in error messages
  -N    disable optimizations
  -S    print assembly listing
  -V    print version and exit
  -W    debug parse tree after type checking
  -allabis
        generate ABI wrappers for all symbols (for bootstrap)
  -asmhdr file
        write assembly header to file
  -bench file
        append benchmark times to file
  -blockprofile file
        write block profile to file
  -buildid id
        record id as the build id in the export metadata
  -c int
        concurrency during compilation, 1 means no concurrency (default 1)
  -complete
        compiling complete package (no C or assembly)
  -cpuprofile file
        write cpu profile to file
  -d list
        print debug information about items in list; try -d help
  -dwarf
        generate DWARF symbols (default true)
  -dwarflocationlists
        add location lists to DWARF in optimized mode (default true)
  -dynlink
        support references to Go symbols defined in other shared libraries
  -e    no limit on number of errors reported
  -gendwarfinl int
        generate DWARF inline info records (default 2)
  -goversion string
        required version of the runtime
  -h    halt on error
  -importcfg file
        read import configuration from file
  -importmap definition
        add definition of the form source=actual to import map
  -installsuffix suffix
        set pkg directory suffix
  -j    debug runtime-initialized variables
  -l    disable inlining
  -lang string
        release to compile for
  -linkobj file
        write linker-specific object to file
  -live
        debug liveness analysis
  -m    print optimization decisions
  -memprofile file
        write memory profile to file
  -memprofilerate rate
        set runtime.MemProfileRate to rate
  -mutexprofile file
        write mutex profile to file
  -nolocalimports
        reject local (relative) imports
  -o file
        write output to file
  -p path
        set expected package import path
  -pack
        write to file.a instead of file.o
  -r    debug generated wrappers
  -race
        enable race detector
  -s    warn about composite literals that can be simplified
  -shared
        generate code that can be linked into a shared library
  -std
        compiling standard library
  -symabis file
        read symbol ABIs from file
  -traceprofile file
        write an execution trace to file
  -trimpath prefix
        remove prefix from recorded source file paths
  -v    increase debug verbosity
  -w    debug type checking
  -wb   enable write barrier (default true)

 

例如,禁用编译器优化和内联优化,你可使用下面的参数:github

$ go build -gcflags="-N -I"

$ go test -v

这个命令能够为测试提供完整的输出。它会打印测试名称、状态(成功或者失败)、测试所耗费的时间,还有测试的日志等等。golang

若是不使用-v参数来测试,输出不多不少,我常常使用-v参数来打开详细测试日志。例子:bootstrap

$ go test -v context
=== RUN   TestBackground
--- PASS: TestBackground (0.00s)
=== RUN   TestTODO
--- PASS: TestTODO (0.00s)
=== RUN   TestWithCancel
--- PASS: TestWithCancel (0.10s)
=== RUN   TestParentFinishesChild
--- PASS: TestParentFinishesChild (0.00s)
=== RUN   TestChildFinishesFirst
--- PASS: TestChildFinishesFirst (0.00s)
=== RUN   TestDeadline
--- PASS: TestDeadline (0.16s)
=== RUN   TestTimeout
--- PASS: TestTimeout (0.16s)
=== RUN   TestCanceledTimeout
--- PASS: TestCanceledTimeout (0.10s)
...
PASS
ok  	context	2.426s

$ go test -race

如今可使用Go工具提供的-race参数进行竞争检测。它会检测并报告竞争。开发的过程当中用这个命令来检测一下。浏览器

注:完整的命令是:app

$ go test -race mypkg    // to test the package
$ go run -race mysrc.go  // to run the source file
$ go build -race mycmd   // to build the command

$ go test -run

你能够在测试的时候经过-run参数来正则匹配过滤须要测试的代码。下面的命令只会运行test examples工具

$ go test -run=Example

$ go test -coverprofile

当测试一个包的时候,能够输出一个测试覆盖率,而后使用命令go tool来在浏览器里面可视化。测试

$ go test -coverprofile=c.out && go tool cover -html=c.out

上面的命令将会建立一个测试覆盖率文件在浏览器打开结果。

注:测试fmt包

go test -coverprofile=c.out fmt

$ go test -exec

通常不多有人知道Go的这个功能,你能够经过-exec插入另外一个程序。这个参数容许经过Go工具完成一些外部工做。

一个常见的需求场景是你须要在一些宿主机上面执行一些测试。咱们能够经过-exec命令调用adb命令来把二进制文件导入安卓设备而且能够收集到结果信息。参考这个来在安卓设备上面执行。

$ go get -u

若是你经过go get命令获取Go包,而这个包已经存在于本地的GOPATH,那么这个命令并不会帮你更新包。-u能够强制更新到最新版。

若是你是一个库做者,你最好在你的安装说明上加上-u参数,例如,golint是这么作的:

$ go get -u github.com/golang/lint/golint

$ go get -d

若是你想clone一个代码仓库到GOPATH里面,跳过编译和安装环节,使用-d参数。这样它只会下载包而且在编译和安装以前中止。

当须要clone虚拟网址代码仓库的时候,我常常使用这个命令来代替git clone,由于这样能够把Go代码自动放入合适的目录下面。例如:

$ go get -d golang.org/x/oauth2/...

这样能够克隆到$GOPATH/src/golang.org/x/ouath2目录下面。假设golang.org/x/oauth2是一个虚拟网址,经过go get获取这个代码仓库要比找出仓库的真实地址(go.googlesource.com/oauth2)更简单。

$ go get -t

若是你的测试包的有附加的依赖包,-t能够一并下载测试包的依赖包。若是没有加这个参数,go get只会下载非测试包的依赖包。

$ go list -f

这个命令能够列出来Go的全部包,而且能够指定格式。这个写脚本的时候颇有用。

下面这个命令将会打印全部依赖的runtime

go list -f ‘’ runtime [runtime/internal/atomic runtime/internal/sys unsafe]

$ go tool compile -S main.go

反编译代码为汇编代码

相关文章
相关标签/搜索