Some unusual command parameter details in Go

  • 2020-06-12 09:19:32
  • OfStack

preface

This article may be biased. This article describes the Go tool parameters that I personally use, along with some of the problems that people around me encounter. If you have any questions, please leave a comment. Are you new to Go? Or do you want to expand your knowledge? This article will describe the Go tool parameters that everyone needs to know. Without further ado, let's take a look at the details.

$ go build -x

-ES11en will be listed go build All commands invoked.

If you're curious about the Go toolchain, or use a cross-ES16en compiler, and want to know the specific parameters used to invoke the external compiler, or suspect that the linker has bug; Use -x to see all calls.


$ 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

This parameter will be passed to the compiler. go tool compile -help It lists all the parameters that we can pass to the compiler.

For example, to disable compiler optimization and inline optimization, you can use the following parameters:


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

$ go test -v

This command provides the complete output for the test. It prints the test name, status (success or failure), time spent on the test, the log of the test, and so on.

If I don't use the -ES33en parameter to test, the output is very small, and I often use the -v parameter to open the 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)
...
PASS
ok context 2.426s

$ go test -race

You can now use the -race parameter provided by the Go tool for competition detection. It detects and reports competition. Use this command to check 1 during development.

Note: The full 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 use the -ES51en parameter to regularly match and filter the code that needs to be tested. The following command will only run test examples.


$ go test -run=Example

$ go test -coverprofile

When testing a package, output 1 test coverage and use the command go tool to visualize in the browser.


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

Note: Test the fmt package


go test -coverprofile=c.out fmt

$ go test -exec

Very few people know this feature of Go. You can insert another program via -ES73en. This parameter allows some external work to be done through the Go tool.

A common requirement scenario is that you need to perform 1 test on 1 host. We can use the -exec command to call the adb command to import the base 2 file into the Android device and collect the result information. Refer to this to execute on an Android device.

$ go get -u

If you get the Go package via the go get command and the package already exists locally on GOPATH, this command will not update the package for you. -u can force updates to the latest version.

If you are a library author, you should add the -ES91en parameter to your installation instructions. For example, golint does this:


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

$ go get -d

If you want clone1 code warehouse to GOPATH, skip compilation and installation and use the -d parameter. This way it will only download the package and stop before compilation and installation.

I often use this command instead of git clone when I need the clone virtual address repository because it automatically places Go code under the appropriate directory. Such as:


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

So that we can clone to $GOPATH/src/golang org x/ouath2 directories below. Hypothesis golang. org x/oauth2 is a virtual web site, through go get to obtain this code than to find out the real warehouse warehouse address (go. googlesource. com/oauth2) easier.

$ go get -t

If your test pack has additional dependencies, -t can 1 and download the test pack dependencies. Without this parameter, go get will only download dependent packages that are not test packages.

$ go list -f

This command lists all packages of Go, and you can specify 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]

Reference link:

http://blog.csdn.net/erlib/article/details/52703165

conclusion


Related articles: