-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 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"
這個命令能夠爲測試提供完整的輸出。它會打印測試名稱、狀態(成功或者失敗)、測試所耗費的時間,還有測試的日誌等等。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工具提供的-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
你能夠在測試的時候經過-run
參數來正則匹配過濾須要測試的代碼。下面的命令只會運行test examples。工具
$ go test -run=Example
當測試一個包的時候,能夠輸出一個測試覆蓋率,而後使用命令go tool
來在瀏覽器裏面可視化。測試
$ go test -coverprofile=c.out && go tool cover -html=c.out
上面的命令將會建立一個測試覆蓋率文件在瀏覽器打開結果。
注:測試fmt包
go test -coverprofile=c.out fmt
通常不多有人知道Go的這個功能,你能夠經過-exec
插入另外一個程序。這個參數容許經過Go工具完成一些外部工做。
一個常見的需求場景是你須要在一些宿主機上面執行一些測試。咱們能夠經過-exec
命令調用adb
命令來把二進制文件導入安卓設備而且能夠收集到結果信息。參考這個來在安卓設備上面執行。
若是你經過go get
命令獲取Go包,而這個包已經存在於本地的GOPATH
,那麼這個命令並不會幫你更新包。-u
能夠強制更新到最新版。
若是你是一個庫做者,你最好在你的安裝說明上加上-u
參數,例如,golint是這麼作的:
$ go get -u github.com/golang/lint/golint
若是你想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)更簡單。
若是你的測試包的有附加的依賴包,-t
能夠一併下載測試包的依賴包。若是沒有加這個參數,go get
只會下載非測試包的依賴包。
這個命令能夠列出來Go的全部包,而且能夠指定格式。這個寫腳本的時候頗有用。
下面這個命令將會打印全部依賴的runtime
包
go list -f ‘’ runtime [runtime/internal/atomic runtime/internal/sys unsafe]
反編譯代碼爲彙編代碼