-x
會列出來go build
調(diào)用到的所有命令。
如果你對Go的工具鏈好奇,或者使用了一個跨C編譯器,并且想知道調(diào)用外部編譯器用到的具體參數(shù),或者懷疑鏈接器有bug;使用-x
來查看所有調(diào)用。
$ go build -xWORK=/var/folders/00/1b8h8000h01000cxqpysvccm005d21/T/go-build600909754mkdir -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.gocd ./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.amv $WORK/hello/perf/_obj/exe/a.out perf
這個參數(shù)將會傳遞給編譯器。go tool compile -help
列出來了所有我們可以傳遞給編譯器的參數(shù)。
$ go tool compile -helpusage: 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)
?
例如,禁用編譯器優(yōu)化和內(nèi)聯(lián)優(yōu)化,你可以使用下面的參數(shù):
$ go build -gcflags="-N -I"
這個命令可以為測試提供完整的輸出。它會打印測試名稱、狀態(tài)(成功或者失敗)、測試所耗費(fèi)的時間,還有測試的日志等等。
如果不使用-v
參數(shù)來測試,輸出很少很多,我經(jīng)常使用-v
參數(shù)來打開詳細(xì)測試日志。例子:
$ 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)...PASSok context 2.426s
現(xiàn)在可以使用Go工具提供的-race
參數(shù)進(jìn)行競爭檢測。它會檢測并報告競爭。開發(fā)的過程中用這個命令來檢測一下。
注:完整的命令是:
$ 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
參數(shù)來正則匹配過濾需要測試的代碼。下面的命令只會運(yùn)行test examples。
$ go test -run=Example
當(dāng)測試一個包的時候,可以輸出一個測試覆蓋率,然后使用命令go tool
來在瀏覽器里面可視化。
$ go test -coverprofile=c.out && go tool cover -html=c.out
上面的命令將會創(chuàng)建一個測試覆蓋率文件在瀏覽器打開結(jié)果。
注:測試fmt包
go test -coverprofile=c.out fmt
一般很少有人知道Go的這個功能,你可以通過-exec
插入另一個程序。這個參數(shù)允許通過Go工具完成一些外部工作。
一個常見的需求場景是你需要在一些宿主機(jī)上面執(zhí)行一些測試。我們可以通過-exec
命令調(diào)用adb
命令來把二進(jìn)制文件導(dǎo)入安卓設(shè)備并且可以收集到結(jié)果信息。參考這個來在安卓設(shè)備上面執(zhí)行。
如果你通過go get
命令獲取Go包,而這個包已經(jīng)存在于本地的GOPATH
,那么這個命令并不會幫你更新包。-u
可以強(qiáng)制更新到最新版。
如果你是一個庫作者,你最好在你的安裝說明上加上-u
參數(shù),例如,golint是這么做的:
$ go get -u github.com/golang/lint/golint
如果你想clone一個代碼倉庫到GOPATH
里面,跳過編譯和安裝環(huán)節(jié),使用-d
參數(shù)。這樣它只會下載包并且在編譯和安裝之前停止。
當(dāng)需要clone虛擬網(wǎng)址代碼倉庫的時候,我經(jīng)常使用這個命令來代替git clone
,因為這樣可以把Go代碼自動放入合適的目錄下面。例如:
$ go get -d golang.org/x/oauth2/...
這樣可以克隆到$GOPATH/src/golang.org/x/ouath2
目錄下面。假設(shè)golang.org/x/oauth2
是一個虛擬網(wǎng)址,通過go get
獲取這個代碼倉庫要比找出倉庫的真實地址(go.googlesource.com/oauth2)更簡單。
如果你的測試包的有附加的依賴包,-t
可以一并下載測試包的依賴包。如果沒有加這個參數(shù),go get
只會下載非測試包的依賴包。
這個命令可以列出來Go的所有包,并且可以指定格式。這個寫腳本的時候很有用。
下面這個命令將會打印所有依賴的runtime
包
go list -f ‘’ runtime [runtime/internal/atomic runtime/internal/sys unsafe]
來源:http://www.icode9.com/content-4-222651.html