go — The Go Command-Line Tool

Practical guide to the go toolchain: build, run, test and mod for modules, workspaces, cross-compiling and formatting.

go is the single command-line tool that bundles the entire Go toolchain: you compile and run programs with go run and go build, run tests and benchmarks with go test, and manage your dependencies through Go modules (go mod). Instead of a separate build system or package manager, everything lives in this one binary – including formatting, static analysis, code generation and profiling. This guide walks you through the commands you reach for daily, from your first go run main.go to cross-compiling and module maintenance.

Build & Run

go run <file> — Compile and run a Go program.

go run main.go

go run . — Compile and run the current package.

go run .

go build — Compile the current package.

go build

go build -o <name> — Compile with a custom output binary name.

go build -o myapp

GOOS=<os> GOARCH=<arch> go build — Cross-compile for a different OS/architecture.

GOOS=linux GOARCH=amd64 go build -o myapp-linux

go install — Compile and install the binary to $GOPATH/bin.

go install

Modules

go mod init <module> — Initialize a new Go module.

go mod init github.com/user/myapp

go mod tidy — Add missing and remove unused dependencies.

go mod tidy

go get <package> — Add or update a dependency.

go get github.com/gin-gonic/gin@latest

go get <package>@<version> — Get a specific version of a dependency.

go get github.com/gin-gonic/gin@v1.9.1

go mod download — Download all dependencies to local cache.

go mod download

go mod vendor — Copy dependencies into a vendor directory.

go mod vendor

go mod graph — Print the module dependency graph.

go mod graph

Test

go test — Run tests in the current package.

go test

go test ./... — Run tests in all packages recursively.

go test ./...

go test -v — Run tests with verbose output.

go test -v

go test -run <regex> — Run only tests matching a pattern.

go test -run TestParse

go test -cover — Show test coverage percentage.

go test -cover ./...

go test -coverprofile=cover.out — Generate coverage profile.

go test -coverprofile=cover.out && go tool cover -html=cover.out

go test -bench . — Run benchmarks.

go test -bench . -benchmem

Format & Lint

go fmt ./... — Format all Go source files.

go fmt ./...

gofmt -d <file> — Show formatting diff without applying changes.

gofmt -d main.go

go vet ./... — Run static analysis to find potential bugs.

go vet ./...

go generate ./... — Run go:generate directives in source files.

go generate ./...

Tools & Environment

go env — Show all Go environment variables.

go env

go env GOPATH — Show a specific environment variable.

go env GOROOT

go env -w <KEY>=<value> — Set a Go environment variable persistently.

go env -w GOPROXY=direct

go version — Show the installed Go version.

go version

go doc <package> — Show documentation for a package or symbol.

go doc fmt.Println

go tool pprof <profile> — Analyze a CPU or memory profile.

go tool pprof cpu.prof

Conclusion

go unifies compiler, test runner, package manager and formatter into a single, quick-to-learn tool – and that's exactly what makes day-to-day Go so smooth. Stick to the module commands (go mod init, go mod tidy) and your dependency tree stays clean and reproducible. A word of caution: go install and go get download, compile and run third-party code along with any build steps – only pull modules you trust, and rely on the safety net of GOPROXY and GOSUMDB, which verify versions and guard against silent tampering. When builds behave strangely, a clean cache via go clean -cache often helps.

Further Reading

  • go.dev/doc – official documentation for the language, toolchain and modules
  • Go Modules Reference – reference for modules, versioning, GOPROXY and GOSUMDB
  • artisan – command-line tool for the Laravel PHP framework
  • cargo – build tool and package manager for Rust
  • composer – dependency manager for PHP