Jan 19, 2019

[Go] useful tool chain commands

go build

Lists all the commands go build invokes.
$ go build -x


Used to pass flags to the Go compiler.
go tool compile -help lists all the flags that can
be passed to the compiler.
$ go build -gcflags



go test

Provides chatty output for the testing.
$ go test -v


Go test also supports this flag and reports races.
Use this flag during development to detect the races.
$ go test -race


Filter tests to run by regex and the -run flag.
The following command will only test examples.
$ go test -run=Example


Outputs a cover profile as testing a package,
then use go tool to visualize them on a browser.
$ go test -coverprofile=c.out && go tool cover -html=c.out


This flag allows you to delegate some work to an
external program from the Go tool.
$ go test -exec



go get

If an argument names a module but not a package (because there is no Go source code in the module's root directory), then the install step is skipped for that argument, instead of causing a build failure.
For example 'go get golang.org/x/perf' succeeds even though there is no code corresponding to that import path.

-u forces the tool to sync with the latest version of the repo.
go get grab version in this precedence:

  1. latest tagged release version, such as v0.4.5 or v1.2.3
  2. latest tagged pre-release version, such as v0.0.1-pre1
  3. latest known commit
  4. This default version selection can be overridden by adding an @version suffix to the package argument, as in 'go get golang.org/x/text@v0.3.0'
  5. For modules stored in source control repositories, the version suffix can also be a commit hash, branch identifier, or other syntax known to the source control system, as in 'go get golang.org/x/text@master'
  6. The version suffix @none indicates that the dependency should be removed entirely, downgrading or removing modules depending on it as needed.
  7. The version suffix @latest explicitly requests the latest minor release of the module named by the given path.
    The suffix @upgrade is like @latest but will not downgrade a module if it is already required at a revision or pre-release version newer than the latest released version.
    The suffix @patch requests the latest patch release: the latest released version with the same major and minor version numbers as the currently required version.
    Like @upgrade, @patch will not downgrade a module already required at a newer version.
    If the path is not already required, @upgrade and @patch are equivalent to @latest.

    Note that branches with names that overlap with other module query syntax cannot be selected explicitly.
    For example, the suffix @v2 means the latest version starting with v2, not the branch named v2.

$ go help get
$ go get -u

The -t flag instructs get to consider modules needed to build tests of packages specified on the command line.
$ go get -ut


Just want to clone a repo to your GOPATH and skip
the building and installation phase, use -d.
$ go get -d golang.org/x/oauth2/...


If your package has additional dependencies for tests,
-t will allow you to download them during go-get.
If you don't pass -t, go get will only download the
dependencies for your non-test code.
$ go get -t



go list

https://dave.cheney.net/2014/09/14/go-list-your-swiss-army-knife
$ go list -f




go doc

Show doc of the package/function
$ go doc 'package name'



go tool

Show supported arch
$ go tool dist list -json
https://github.com/golang/go/blob/master/src/go/build/syslist.go
https://golang.org/doc/install/source#environment



others

Since Golang produces elf binary, we could strip it as well like C/C++ linker
$ ld -s

strip (man strip) works well in elf Golang binary.
$ strip -s go_binary

A Golang standard way:
$ go build -ldflags "-w"  // Omit the DWARF symbol table.
$ go build -ldflags "-s"   // Not work on Mac, Omit the symbol table and debug information.



Reference:
https://golang.org/cmd/link/

Use UPX to boost shrinking the binaray size as used for other elf:
$ upx go_binaray  // not good for startup performance
$ upx --ultra-brute go_binary

Print escape analysis
$ go build -gcflags '-m'

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.