go build, run, install & clean
These four commands cover how Go turns source into binaries, executes them locally, installs tools globally, and resets caches when debugging stale builds.
Search across all documentation pages
These four commands cover how Go turns source into binaries, executes them locally, installs tools globally, and resets caches when debugging stale builds.
go build compiles packages into artifacts you ship.
go run is a dev shortcut that compiles to a temp file and runs it immediately.
go install places command binaries on your PATH via GOBIN or GOPATH/bin.
go clean removes build outputs and cache entries when you need a cold rebuild.
All four share the same package loader and honor build tags, GOOS, GOARCH, and module settings.
Quick-reference recipe card - copy-paste ready.
# Build a service binary with size-focused flags
go build -trimpath -ldflags="-s -w" -o bin/api ./cmd/api
# Run locally with arguments
go run ./cmd/api --addr :8080
# Install a CLI tool at the module version in go.mod
go install golang.org/x/tools/cmd/goimports@latest
# Clear test result cache after fixing a flaky test
go clean -testcacheWhen to reach for this:
go build for release artifacts, Docker images, and CI binaries.go run for quick iteration on main packages.go install for developer tools (goimports, staticcheck, codegen CLIs).go clean -cache only when diagnosing corrupted or impossible cache hits.# Module layout:
# .
# ├── go.mod
# ├── cmd/seed/main.go
# └── internal/seed/seed.go
go build -o bin/seed ./cmd/seed
./bin/seed --count 5
go run ./cmd/seed --count 3
go install ./cmd/seed # installs to $(go env GOPATH)/bin or GOBIN
go clean -i ./cmd/seed # remove installed binary for this import path// cmd/seed/main.go
package main
import (
"flag"
"fmt"
"example.com/tooldemo/internal/seed"
)
func main() {
n := flag.Int("count", 1, "records to create")
flag.Parse()
fmt.Println(seed.Generate(*n))
}// internal/seed/seed.go
package seed
import "fmt"
func Generate(n int) string {
return fmt.Sprintf("generated %d records", n)
}What this demonstrates:
go build -o names the output explicitly for deploy scripts.go run accepts the same package path and forwards flags after -- when needed.go install with a relative ./cmd/seed installs from the current module without a separate go get.go clean -i removes the installed binary matching that package's import path.go run links to a temporary executable in the OS temp directory and deletes it after exit (unless -work leaves intermediates for inspection).go install skips copying into the current directory; it only updates the install destination.| Flag | Effect |
|---|---|
-o path | Output file name (build) |
-trimpath | Remove file system paths from recorded positions |
-ldflags "..." | Pass flags to the linker (-s -w strips debug info) |
-tags list | Enable comma-separated build tags |
-race | Enable race detector (build/test) |
-mod vendor | Read dependencies from vendor/ |
-a | Force rebuild of all packages (ignore cache) |
| Invocation | Removes |
|---|---|
go clean | Build artifacts in current package dir |
go clean -cache | Entire build cache (GOCACHE) |
go clean -testcache | Cached pass/fail results from go test |
go clean -modcache | Downloaded modules (GOMODCACHE) |
go clean -i pkg... | Installed binaries/archives for packages |
# Inspect where artifacts go
go env GOCACHE GOMODCACHE GOPATH GOBIN
# Print linker-invoked build ID embedded in binaries
go tool buildid bin/seedgo run in production scripts - compiles on every invocation and hides the artifact path. Fix: go build once, run the binary.-o in CI - default output names collide when building multiple commands in one directory. Fix: always set -o bin/<name>.go install without a version outside a module - @latest may jump ahead of CI pins. Fix: run from a module with a recorded require or pin @v1.2.3.go clean -modcache in shared CI - forces full re-download and slows every job. Fix: cache GOMODCACHE between runs; clean only on corruption errors.GOOS/GOARCH. Fix: disable cgo (CGO_ENABLED=0) or install the matching cross compiler.-trimpath expectations - panics still include function names; only file paths are trimmed. Fix: upload symbols separately if you need path-level debugging.| Alternative | Use When | Don't Use When |
|---|---|---|
make / just wrappers | Teams want named targets (make test) | The wrapper hides go flags you need to learn |
goreleaser | Multi-platform releases with archives and checksums | A single go build for one Linux deploy is enough |
| Bazel / Buck | Hermetic builds at huge scale | Small modules where go build is already fast |
go run | Local iteration on main | Production containers or systemd units |
Debug info and symbol tables add size.
Use -ldflags="-s -w" and -trimpath for release builds.
UPX compression is rarely worth the antivirus false positives.
GOBIN if set, otherwise GOPATH/bin.
Ensure that directory is on your PATH.
Forces rebuilding all packages even when cache hits exist.
Use sparingly; it defeats incremental compilation.
Pass multiple package paths: go build -o bin/a ./cmd/a -o bin/b ./cmd/b does not work as one invocation.
Build each main separately or use a small shell loop.
No.
It removes object files, cached archives, installed binaries, and cache directories - never .go sources.
-trimpath removes compile-time paths.
Combine with private module hosting and GOPRIVATE for fetch-time privacy.
It compiles and links on every invocation.
Keep using it for dev; switch to go build for repeated runs of the same code.
The directory storing build action outputs keyed by inputs.
It makes incremental builds fast and is safe to delete when troubleshooting.
Yes.
GOOS=linux GOARCH=arm64 go build cross-compiles; the same env vars apply to go install.
Enable it for test binaries (go test -race) more often than production binaries.
Race-enabled binaries are slower and need more memory.
Stack versions: This page was written for Go 1.26.x (Green Tea GC default, go fix modernizers - verify patch at build), chi (latest - verify at build), gin (latest - verify at build), echo (latest - verify at build), google.golang.org/grpc (latest - verify at build), sigs.k8s.io/controller-runtime (latest - verify at build), kubebuilder (latest - verify at build), tinygo (latest - verify board targets at build), wazero (latest - verify at build), and golangci-lint (latest - verify linter set at build).
Reviewed by Chris St. John·Last updated Jul 18, 2026