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.gogo run . — Compile and run the current package.
go run .go build — Compile the current package.
go buildgo build -o <name> — Compile with a custom output binary name.
go build -o myappGOOS=<os> GOARCH=<arch> go build — Cross-compile for a different OS/architecture.
GOOS=linux GOARCH=amd64 go build -o myapp-linuxgo install — Compile and install the binary to $GOPATH/bin.
go installModules
go mod init <module> — Initialize a new Go module.
go mod init github.com/user/myappgo mod tidy — Add missing and remove unused dependencies.
go mod tidygo get <package> — Add or update a dependency.
go get github.com/gin-gonic/gin@latestgo get <package>@<version> — Get a specific version of a dependency.
go get github.com/gin-gonic/gin@v1.9.1go mod download — Download all dependencies to local cache.
go mod downloadgo mod vendor — Copy dependencies into a vendor directory.
go mod vendorgo mod graph — Print the module dependency graph.
go mod graphTest
go test — Run tests in the current package.
go testgo test ./... — Run tests in all packages recursively.
go test ./...go test -v — Run tests with verbose output.
go test -vgo test -run <regex> — Run only tests matching a pattern.
go test -run TestParsego 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.outgo test -bench . — Run benchmarks.
go test -bench . -benchmemFormat & Lint
go fmt ./... — Format all Go source files.
go fmt ./...gofmt -d <file> — Show formatting diff without applying changes.
gofmt -d main.gogo 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 envgo env GOPATH — Show a specific environment variable.
go env GOROOTgo env -w <KEY>=<value> — Set a Go environment variable persistently.
go env -w GOPROXY=directgo version — Show the installed Go version.
go versiongo doc <package> — Show documentation for a package or symbol.
go doc fmt.Printlngo 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,
GOPROXYandGOSUMDB