GO Commands Cheat Sheet

Here is a cli commands cheat sheet for GO command, you can use this as a quick reminder for basic commands with a brief description for each of the commands.

What is GO command?

Add some data here

        # To run the package in the current directory
go run .

# To run the package in the ./cmd/foo directory
go run ./cmd/foo/

# To fetch dependencies
go get github.com/foo/[email protected]
go get github.com/foo/bar@8e1b8d3

# Upgrade the dependency.
go get -u github.com/foo/bar

# Building an Executable. Compile the package in the current directory
go build -o=/tmp/foo .

# Compile the package in the ./cmd/foo directory
go build -o=/tmp/foo ./cmd/foo 

# Check where your build cache is
go env GOCACHE 

# Force all packages to be rebuilt
go build -a -o=/tmp/foo . 

# Remove everything from the build cache
go clean -cache 


# Cross-Compilation
GOOS=linux GOARCH=amd64 go build -o=/tmp/linux_amd64/foo .
GOOS=windows GOARCH=amd64 go build -o=/tmp/windows_amd64/foo.exe .

# List of all supported OS/architectures
go tool dist list

# To show all the dependencies of the module
go list -m all

# To sho why is that a dependency?
go mod why -m golang.org/x/sys

# Clear module cache
go clean -modcache

# View simplified documentation for the strings package
go doc strings

# View full documentation for the strings package
go doc -all strings

# View documentation for the strings.Replace function
go doc strings.Replace

# View documentation for the database/sql.DB type
go doc sql.DB

# View documentation for the database/sql.DB.Query method
go doc sql.DB.Query

# View the source code for the strings.Replace function
go doc -src strings.Replace

# Run all tests in the current directory
go test .

# Run all tests in the current directory and sub-directories
go test ./...

# Run all tests in the ./foo/bar directory
go test ./foo/bar

# Testing with race detector
go test -race ./...

# Bypass the test cache when running tests
go test -count=1 ./...

# Delete all cached test results
go clean -testcache

# Run the test with the exact name TestFooBar
go test -v -run=^TestFooBar$ .

# Run tests whose names start with TestFoo
go test -v -run=^TestFoo .

# Run the Baz subtest of the TestFooBar test only
go test -v -run=^TestFooBar$/^Baz$ .

# Handy flag - skip long running tests
go test -short ./...

# Handy flag - don't run further tests after a failure.
go test -failfast ./... 

# Test all module dependencies.
go test all

# Stress testing
go test -run=^TestFooBar$ -count=500 .

# Upgrading the code to a New Go Release
go fix ./...

# Create a new Github issue for Go's standard library
go bug 

# Trace generation
go test -run=^$ -bench=^BenchmarkFoo$ -trace=/tmp/trace.out .

# Works only on Chrome / Chromium at the moment
go tool trace /tmp/trace.out 

# List all environment variables.
go env 

# Set GOPATH environment variable to /foo/bar
go env -w GOPATH=/foo/bar 

# Checking for Race Conditions
go build -race -o=/tmp/foo . # not for production
    

Check out the GO command documentation .


You can also check our MegaSh cheatsheet tool, that has 150+ searchable linux cheat sheets in one page, so you never forget a command as you work again

Check Also

Best AI tools list