go commands cheatsheet.
Build, run, test and manage modules — the go commands you use every day. Tap to copy.
go run .Compile and run the package in the current directorygo buildCompile the package into an executablego build -o bin/appCompile to a named output pathgo installCompile and install the binary to GOBINgo mod init <module>Start a new module and create go.modgo mod tidyAdd missing and remove unused dependenciesgo get <pkg>@<version>Add or update a dependency at a versiongo mod downloadDownload modules to the local cachego mod vendorCopy dependencies into a vendor directorygo test ./...Run all tests in the modulego test -run <name>Run only tests matching a name patterngo test -v -coverVerbose output with coverage percentagego test -bench .Run benchmarks in the current packagego fmt ./...Format all source files with gofmtgo vet ./...Report suspicious constructs in your codego doc <pkg>Show documentation for a packagego envPrint the Go environment configurationgo versionPrint the installed Go versiongo cleanRemove build artifacts and object filesgo list ./...List all packages in the modulego work initCreate a go.work file for a multi-module workspaceWhat 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.