Go Tooling Cheat Sheet
Go Tooling Cheat Sheet
Go Tooling Cheat Sheet
$ 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.