More useful flags for GO tools

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Did you just touch go tools? Or do you want to expand your knowledge? This article is about the flags of Go tools, and these flags should be known to everyone.

Disclaimer: This document may have some prejudice. This is my personal favorite flags collection. It's hard for people around me to find these Falgs reference documents. If you have a better idea, you can have a private messages on Twitter.

$ go build-x

-xLists all the commands that go build triggers.

Use to view all triggers if you are curious about the tool chain of go, the use of cross-platform compilers, or the flags for incoming external compilers are unclear, or if you suspect that the linker has a bug -x .

$ go build-x

  work=/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

Used to pass parameters to the go compiler. go tool compile -helplists all the parameter lists that can be passed into the compiler.

For example, to suppress compiler optimizations and inline, you can use the following gcfalgs:

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

$go test-v

It provides an informal test output that prints the name, status (pass or fail) of the test, time-consuming, log of the test case, and so on.

-vgo test without flag is very quiet, I often turn on the -v switch. For example, the output is as follows:

  $ go test-v context=== run testbackground---pass:testbackground (0.00s) = = = Run Testtodo---pass:testtod O (0.00s) = = = Run Testwithcancel---pass:testwithcancel (0.10s) = = = Run Testparentfinisheschild---pass:testparentfini Sheschild (0.00s) = = = Run Testchildfinishesfirst---pass:testchildfinishesfirst (0.00s) = = = Run Testdeadline---pass:t Estdeadline (0.16s) = = = Run Testtimeout---pass:testtimeout (0.16s) = = = Run Testcanceledtimeout---pass:testcanceledti Meout (0.10s) ... Passok Context 2.426s  

$go Test-race

Go competitive detection Tool can be --race used. Go test also supports this flag and reports the competition. This flag can be used to detect competition during the development phase.

$go Test-run

With -run flag, you can filter test cases by regular. The following command only tests test examples:

$ go test -run=Example

$go Test-coverprofile

You can output a overwrite message if you are testing a package and then use the Go tool to visualize it on the browser:

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

The above command creates a overwrite message and then opens the results page on the browser. The results will look similar to the following pages:

$go test-exec

This is a little-known feature, using -exec this flag, you can interact with other programs and tools. This flag allows you to use the Go tool to delegate some work to another program.

The common requirement scenario for using this flag is when you need to do more than just execute the host program. Go's Android build, used -exec to push test binaries to Android devices (by using adb ), and collect test results. Can be used as a reference.

$go Get-u

If you execute the go-test command to get a package that is already in Gopath, then the go-get is not good to update the package to the latest version, but -u will force the tool to synchronize the latest version of the repository.

If you are the author of a library, then you may like to write your installation instructions through -u flag, for example, golint in such a way:

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

$go get-d

If you only want to clone a repo to Gopath, skip the compile and install process, then use -d . It downloads the package and then stops before attempting to compile and install.

I often use it as an alternative to Git clone, using a fake URLs because it clones this repo to its proper gopath. Like what:

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

Will clone the package to $GOPATH/src/golang.org/x/ouath2 . Given golang.org/x/oauth2 is a false url,go-get this warehouse is very useful, instead of trying to know what a confidant repo is (go.googlesource.com/oauth2).

$go get-t

If your package requires additional packages to test, it -t will allow you to download them during the go-get process. If you do not pass in -t parameters, go get will only download dependencies for non-test code.

$ go list-f

Allows you to download go packages in a custom format. Very useful for writing bash scripts. The following command outputs the runtime package dependencies:

$ go list -f '{{.Deps}}' runtime[runtime/internal/atomic runtime/internal/sys unsafe]

More formatting information can be found in the Go list section of Dave Cheney's article.

English original

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.