This is a creation in Article, where the information may have evolved or changed.
Are you just getting started with the Go tool? Or do you want to expand your knowledge? This article will describe the go tool parameters that everyone needs to know.
Disclaimer: This article may be biased. This article describes the Go tool parameters that I personally use, and some of the problems that people around me have encountered. If you have any other ideas, contact me on Twitter.
$ go build-x
-xAll commands that are called are listed go build .
If you are curious about the toolchain of go, or if you are using a cross-c compiler, and want to know the specific parameters that are used to invoke the external compiler, or suspect that the linker has a bug, use -x to view all calls.
$ 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
$ go build-gcflags
This parameter will be passed to the compiler. go tool compile -helplists all the parameters that we can pass to the compiler.
For example, to disable compiler optimizations and inline optimizations, you can use the following parameters:
$ go build -gcflags="-N -I"
$ go test-v
This command provides a complete output for the test. It prints the name of the test, the status (success or failure), the time spent on the test, the log of the test, and so on.
If you do not use -v parameters to test, the output is very small, I often use -v parameters to open a detailed test log. Example:
$ 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 context2.426s
$ go test-race
You can now use the parameters provided by the Go tool -race for competitive detection. It detects and reports the competition. The process of development with this command to check.
Note: The complete command is:
$ 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
You can filter the code that needs to be tested by using -run parameters to match regular matches at test time. The following command will only run test examples.
$ go test -run=Example
$ go test-coverprofile
When testing a package, you can output a test coverage and then use the command go tool to visualize it in the browser.
$ go test -coverprofile=c.out && go tool cover -html=c.out
The above command will create a test coverage file in the browser to open the results. The results of the visualization appear to be the following:
Note: Test the FMT Package
go test -coverprofile=c.out fmt
$ go test-exec
Generally very few people know this function of go, you can -exec insert another program by inserting it. This parameter allows some external work to be done through the Go tool.
A common requirement scenario is that you need to perform some tests on a host of hosts. We can import the binary files into the Android -exec adb device and collect the result information through the command call command. Refer to this to perform on Android devices.
$ go get-u
If you go get get the Go package by command and the package already exists locally GOPATH , this command will not help you update the package. -uYou can force updates to the latest version.
If you are a library author, you might want to add parameters to your installation instructions -u , for example, Golint:
$ go get -u github.com/golang/lint/golint
$ go get-d
If you want to clone a code repository into the GOPATH inside, skip the compile and install links, using the -d parameters. This will only download the package and stop before compiling and installing.
When I need the clone virtual URL code warehouse, I often use this command instead git clone , because it will automatically put the go code into the appropriate directory. For example:
$ go get -d golang.org/x/oauth2/...
This can be cloned to the $GOPATH/src/golang.org/x/ouath2 directory below. Suppose golang.org/x/oauth2 It is a virtual URL, it is go get easier to get this code repository than to find the real address (go.googlesource.com/oauth2) of the warehouse.
$ go get-t
If you have additional dependencies for your test package, you -t can download the dependent packages for the test package as a bundle. If this parameter is not added, go get only the dependent packages of the non-test package will be downloaded.
$ go list-f
This command lists all packages to go and can be specified in the format. This is useful when writing scripts.
The following command will print all dependent runtime packages
Go list-f ' runtime[runtime/internal/atomic runtime/internal/sys unsafe]
More formatting methods can further read Dave Cheney's article.
Original link: "Translation" Go tool points, reproduced please specify the source!