Go Commands

go commands cheatsheet.

Build, run, test and manage modules — the go commands you use every day. Tap to copy.

Build & run
go run .Compile and run the package in the current directory
go buildCompile the package into an executable
go build -o bin/appCompile to a named output path
go installCompile and install the binary to GOBIN
Modules
go mod init <module>Start a new module and create go.mod
go mod tidyAdd missing and remove unused dependencies
go get <pkg>@<version>Add or update a dependency at a version
go mod downloadDownload modules to the local cache
go mod vendorCopy dependencies into a vendor directory
Test
go test ./...Run all tests in the module
go test -run <name>Run only tests matching a name pattern
go test -v -coverVerbose output with coverage percentage
go test -bench .Run benchmarks in the current package
Format & inspect
go fmt ./...Format all source files with gofmt
go vet ./...Report suspicious constructs in your code
go doc <pkg>Show documentation for a package
go envPrint the Go environment configuration
Tools & info
go versionPrint the installed Go version
go cleanRemove build artifacts and object files
go list ./...List all packages in the module
go work initCreate a go.work file for a multi-module workspace

What the go tool actually does

The go command is the single CLI that ships with a Go installation, and it wraps almost everything you do day to day: compiling, running, testing, and dependency management all live under one binary. At the centre of a project is the go.mod file, which names your module and lists its dependencies. Because the compiler is fast, the loop of go run . to try something and go build to produce a binary feels near-instant — no separate bundler or build system to configure.

Building and running

There are two ways to get code executing. go run . compiles the current package to a temporary binary, runs it, and throws the binary away — perfect for iterating. go build compiles a reusable executable into the current directory, and go build -o bin/app lets you choose the output path. When you want the binary installed onto your PATH, go install compiles it and drops the result in your GOBIN directory so the command is available everywhere.

Modules and dependencies

Dependency management is handled by the go mod subcommands. go mod init <module> starts a new module and writes go.mod, go get <pkg>@<version> adds or upgrades a specific dependency, and go mod tidy is the one you run constantly — it adds anything your imports require and removes what you no longer use, keeping go.mod and go.sum honest. For reproducible or offline builds, go mod download and go mod vendor pull dependencies into the cache or a local vendor folder.

Testing and keeping code clean

Testing is built in: go test ./... runs every test in the module, go test -run <name> narrows to a pattern, and go test -bench . runs benchmarks. Alongside that, go fmt ./... formats your code to the canonical style and go vet ./... catches suspicious constructs before they become bugs. These pair naturally with the Git commands for version control and the Docker commands when you containerise your app.

FAQ

What does go mod tidy do?
go mod tidy syncs go.mod and go.sum with your source: it adds any modules the code imports and removes ones that are no longer used, so your dependency list matches reality.
What's the difference between go run and go build?
go run compiles and immediately executes the program from a temporary binary without leaving anything behind; go build compiles and writes a reusable executable to disk that you can run or ship.

More cheatsheets