[go: up one dir, main page]

0% found this document useful (0 votes)
61 views2 pages

Go Tooling Cheat Sheet

This document provides instructions for various Go tooling commands used for installing tools, profiling test coverage, stress testing, viewing environment information, running and testing code, fetching dependencies, refactoring code, viewing documentation, testing, linting, and tidying dependencies. Some key commands include go get to install tools, go test for testing, go run to execute code, go doc to view documentation, and go mod to manage modules and dependencies.

Uploaded by

Ali Esam Dabes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views2 pages

Go Tooling Cheat Sheet

This document provides instructions for various Go tooling commands used for installing tools, profiling test coverage, stress testing, viewing environment information, running and testing code, fetching dependencies, refactoring code, viewing documentation, testing, linting, and tidying dependencies. Some key commands include go get to install tools, go test for testing, go run to execute code, go doc to view documentation, and go mod to manage modules and dependencies.

Uploaded by

Ali Esam Dabes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Go's Tooling Cheat Sheet 1/2 

Installing Tooling  Profiling Test Coverage 


$ go test -cover ./... 
$ go test -coverprofile=/tmp/profile.out ./... # coverage profile for browser 
Easiest way  $ go tool cover -html=/tmp/profile.out 
$ go get -u golang.org/x/tools/...  $ go test -covermode=count -coverprofile=/tmp/profile.out ./... # coverage with frequency shown 
$ go tool cover -html=/tmp/profile.out 
Alternative way  $ go test -coverprofile=/tmp/profile.out ./... # coverage in CLI without any browser 
$ go tool cover -func=/tmp/profile.out 
$ cd /tmp # To avoid to install into a local project as a module 
$ GO111MODULE=on go get golang.org/x/tools/cmd/stress  Stress Testing 

Viewing Environment Information  $ go test -run=^TestFooBar$ -count=500 . 


$ go test -c -o=/tmp/foo.test . # using stress tool 
$ go env  $ stress -p=4 /tmp/foo.test -test.run=^TestFooBar$ 
$ go env GOPATH GOOS GOARCH 
$ go help environment  Testing all dependencies 

$ go test all
Development 
Pre-Commit Checks 
Running Code 
$ go run . # Run the package in the current directory  Formatting code 
$ go run ./cmd/foo # Run the package in the ./cmd/foo directory 
$ gofmt -w -s -d foo.go # Format the foo.go file 
Fetching Dependencies  $ gofmt -w -s -d . # Recursively format everything 
$ go fmt ./... # alternative formatting tool 
$ go get github.com/foo/bar@v1.2.3 
$ go get github.com/foo/bar@8e1b8d3  Performing Static Analysis with vet 
$ go list -m all # Show all the dependencies 
$ go mod why -m golang.org/x/sys # Why is that a dependency?  $ go vet foo.go # Vet the foo.go file 
$ go clean -modcache # clear module cache  $ go vet . # Vet all files in the current directory 
$ go vet ./... # Vet all files in the current directory and sub-directories 
Refactoring Code  $ go vet ./foo/bar # Vet all files in the ./foo/bar directory 
$ go vet -composites=false ./... # Disable some analyzers 
gofmt -d -w -r 'foo -> Foo' .  Replace foo by Foo 
gofmt -w -r 'strings.Replace(a, b, -1) -> strings.ReplaceAll(a, b)' .    Experimental analyzers 

$ cd /tmp 
Viewing Go Documentation  $ GO111MODULE=on go get golang.org/x/tools/go/analysis/passes/nilness/cmd/nilness 
$ go doc strings # View simplified documentation for the strings package  $ GO111MODULE=on go get golang.org/x/tools/go/analysis/passes/shadow/cmd/shadow 
$ go doc -all strings # View full documentation for the strings package  $ go vet -vettool=$(which nilness) ./... 
$ go doc strings.Replace # View documentation for the strings.Replace function 
$ go doc sql.DB # View documentation for the database/sql.DB type  Disable vet checks before running any tests 
$ go doc sql.DB.Query # View documentation for the database/sql.DB.Query method  $ go test -vet=off ./... 
$ go doc -src strings.Replace # View the source code for the strings.Replace function 
Linting Code 
Testing  $ cd /tmp # installing the linter 
$ GO111MODULE=on go get golang.org/x/lint/golint 
Running Tests  $ golint foo.go # Lint the foo.go file 
$ golint . # Lint all files in the current directory 
$ go test . # Run all tests in the current directory  $ golint ./... # Lint all files in the current directory and sub-directories 
$ go test ./... # Run all tests in the current directory and sub-directories  $ golint ./foo/bar # Lint all files in the ./foo/bar directory 
$ go test ./foo/bar # Run all tests in the ./foo/bar directory 
$ go test -race ./... # Testing with race detector  Tidying and verifying Your dependencies 
$ go test -count=1 ./... # Bypass the test cache when running tests 
$ go clean -testcache # Delete all cached test results  $ go mod tidy # prune any unused dependencies 
$ go mod verify # check the dependencies' hashes
$ go test -v -run=^TestFooBar$ . # Run the test with the exact name TestFooBar 
$ go test -v -run=^TestFoo . # Run tests whose names start with TestFoo 
$ go test -v -run=^TestFooBar$/^Baz$ . # Run the Baz subtest of the TestFooBar test only 
$ go test -short ./... # handy flag - skip long running tests 
$ go test -failfast ./... # handy flag - don't run further tests after a failure.

Go's Tooling Cheat Sheet - v.1.0.2 - 2019-04-18  


2019 Fedir Rykhtik // ​@FedirFr // CC BY 4.0 // ​https://creativecommons.org/licenses/by/4.0/ 
2019 Alex Edwards // ​@ajmedwards // ​https://www.alexedwards.net/blog/an-overview-of-go-tooling​ // MIT // ​https://opensource.org/licenses/MIT 
Go's Tooling Cheat Sheet 2/2 

Build and Deployment  Profiling and Tracing 


Building an Executable  Running and comparing benchmarks 
$ go build -o=/tmp/foo . # Compile the package in the current directory  $ go test -run=^$ -bench=^BenchmarkFoo$ -cpuprofile=/tmp/cpuprofile.out . 
$ go build -o=/tmp/foo ./cmd/foo # Compile the package in the ./cmd/foo directory  $ go test -run=^$ -bench=^BenchmarkFoo$ -memprofile=/tmp/memprofile.out . 
$ go test -run=^$ -bench=^BenchmarkFoo$ -blockprofile=/tmp/blockprofile.out . 
Build cache  $ go test -run=^$ -bench=^BenchmarkFoo$ -mutexprofile=/tmp/mutexprofile.out . 
$ go test -run=^$ -bench=^BenchmarkFoo$ -o=/tmp/foo.test -cpuprofile=/tmp/cpuprofile.out . 
$ go env GOCACHE # Check where your build cache is  $ go tool pprof -http=:5000 /tmp/cpuprofile.out # inspecting a profile in a browser 
$ go build -a -o=/tmp/foo . # Force all packages to be rebuilt  $ go tool pprof --nodefraction=0.1 -http=:5000 /tmp/cpuprofile.out # ignore nodes smaller than 10% 
$ go clean -cache # Remove everything from the build cache   
Cross-Compilation  Trace generation 
$ GOOS=linux GOARCH=amd64 go build -o=/tmp/linux_amd64/foo .  $ go test -run=^$ -bench=^BenchmarkFoo$ -trace=/tmp/trace.out . 
$ GOOS=windows GOARCH=amd64 go build -o=/tmp/windows_amd64/foo.exe .  $ go tool trace /tmp/trace.out # Works only on Chrome / Chromium at the moment 
$ go tool dist list # list of all supported OS/architectures 
Checking for Race Conditions 
Using Compiler and Linker Flags 
$ go build -race -o=/tmp/foo . # not for production 
$ go tool compile -help # complete list of available compiler flags  $ GORACE="log_path=/tmp/race" /tmp/foo # specify the output to a fiel instead of stderr
$ go build -gcflags="-m -m" -o=/tmp/foo . # print optimization decisions 
$ go build -gcflags="all=-m" -o=/tmp/foo . # optimization decisions for dependencies too 
$
$
go build -gcflags="all=-N -l" -o=/tmp/foo . # disable optimizations and inlining 
go tool link -help # list of available linker flags 
Managing Dependencies 
$ go build -ldflags="-X main.version=1.2.3" -o=/tmp/foo . # add a version number 
$ go build -ldflags="-s -w" -o=/tmp/foo . # strip debug information from the binary  $ go list -m -u github.com/alecthomas/chroma # check for updates to all dependencies 
$ CGO_ENABLED=0 GOOS=linux go build -a -ldflags '-extldflags "-static"' . # make the binary as static as possible 
Upgrade (or downgrade) a dependency 

Diagnosing Problems and Making Optimizations  $ go get github.com/foo/bar@latest 


$ go get github.com/foo/bar@v1.2.3 
$ go get github.com/foo/bar@7e0369f 
Running and Comparing Benchmarks 
Run the tests for all packages to help check for incompatibilities 
$ go test -bench=. ./... # Run all benchmarks and tests 
$ go test -run=^$ -bench=. ./... # Run all benchmarks (and no tests)  $ go mod tidy 
$ go test -run=^$ -bench=^BenchmarkFoo$ ./... # Run only the BenchmarkFoo benchmark (and no tests)  $ go test all 
$ go test -bench=. -benchmem ./... # Forces the output of memory allocation statistics 
Use a local version of a dependency 
$ go test -bench=. -benchtime=5s ./... # Run each benchmark test for at least 5 seconds 
$ go test -bench=. -benchtime=500x ./... # Run each benchmark test for exactly 500 iterations  $ go mod edit -replace=github.com/alexedwards/argon2id=/home/alex/code/argon2id # create the replace rule 
$ go test -bench=. -count=3 ./... # Repeat each benchmark test 3 times over  $ go mod edit -dropreplace=github.com/alexedwards/argon2id # remove the replace rule 
$ go test -bench=. -cpu=1,4,8 ./... # Run benchmarks with GOMAXPROCS set to 1, 4 and 8 

Comparing changes between benchmarks  Upgrading the code to a New Go Release 


$ cd /tmp # Installing 
$ GO111MODULE=on go get golang.org/x/tools/cmd/benchcmp  $ go fix ./... 
$ go test -run=^$ -bench=. -benchmem ./... > /tmp/old.txt 
#
$
make changes 
go test -run=^$ -bench=. -benchmem ./... > /tmp/new.txt  Reporting Bugs 
$ benchcmp /tmp/old.txt /tmp/new.txt 
$ go bug # create a new Github issue for Go's standard library 

Go's Tooling Cheat Sheet - v.1.0.2 - 2019-04-18  


2019 Fedir Rykhtik // ​@FedirFr // CC BY 4.0 // ​https://creativecommons.org/licenses/by/4.0/ 
2019 Alex Edwards // ​@ajmedwards // ​https://www.alexedwards.net/blog/an-overview-of-go-tooling​ // MIT // ​https://opensource.org/licenses/MIT 

You might also like