This section provides a comprehensive reference for the most commonly used Go commands. Understanding these commands is essential for effective Go development.
Compiles Go packages and dependencies.
Basic usage
go build # Build current package
go build ./... # Build all packages
go build -o output main.go # Specify output file
Common flags
-o: Specify output file name-v: Verbose output-x: Print commands as they execute-race: Enable race detector-tags: Build tags to use-ldflags: Linker flags-gcflags: Compiler flagsWhy go build is essential
Examples
# Build current package
go build
# Build with race detector
go build -race
# Build with custom output
go build -o myapp main.go
# Build with build tags
go build -tags dev
Compiles and runs Go programs in one step.
Basic usage
go run main.go # Run single file
go run . # Run current package
go run ./cmd/server # Run specific package
Common flags
-race: Enable race detector-tags: Build tagsWhy go run is useful
Examples
# Run main.go
go run main.go
# Run with race detector
go run -race main.go
# Run with build tags
go run -tags dev main.go
Tests Go packages.
Basic usage
go test # Run tests in current package
go test ./... # Run all tests
go test -v # Verbose output
go test -run TestName # Run specific test
Common flags
-v: Verbose output-run: Run tests matching pattern-bench: Run benchmarks-cover: Show coverage-race: Enable race detector-tags: Build tags-timeout: Test timeoutWhy go test is powerful
Examples
# Run all tests
go test
# Run specific test
go test -run TestAdd
# Run with coverage
go test -cover
# Run benchmarks
go test -bench=.
# Run with race detector
go test -race
Module maintenance commands.
go mod init
go mod init example.com/project # Initialize new module
Why go mod init
go.mod filego mod tidy
go mod tidy # Add missing and remove unused modules
Why go mod tidy
go mod download
go mod download # Download modules to cache
go mod verify
go mod verify # Verify module checksums
go mod graph
go mod graph # Print module dependency graph
go mod why
go mod why -m package # Explain why package is needed
Why go mod commands matter
Adds dependencies to current module.
Basic usage
go get package@version # Get specific version
go get package@latest # Get latest version
go get -u ./... # Update all dependencies
Common flags
-u: Update to latest version-d: Download only, don't build-t: Also download test dependenciesWhy go get is important
Examples
# Get latest version
go get github.com/example/package
# Get specific version
go get github.com/example/[email protected]
# Update all dependencies
go get -u ./...
# Download only
go get -d github.com/example/package
Compiles and installs packages.
Basic usage
go install package@version # Install package
go install ./cmd/tool # Install local package
Why go install is useful
Examples
# Install tool
go install golang.org/x/tools/cmd/goimports@latest
# Install local command
go install ./cmd/myapp
Formats Go code.
Basic usage
go fmt ./... # Format all Go files
go fmt path/to/file.go # Format specific file
Why go fmt is essential
Examples
# Format current package
go fmt
# Format all packages
go fmt ./...
# Format specific file
go fmt main.go
Reports suspicious code constructs.
Basic usage
go vet ./... # Check all packages
go vet package # Check specific package
Why go vet is valuable
Examples
# Check current package
go vet
# Check all packages
go vet ./...
# Check with build tags
go vet -tags dev ./...
Shows documentation for packages and symbols.
Basic usage
go doc package # Package documentation
go doc package.Function # Function documentation
go doc -all package # All documentation
Why go doc is helpful
Examples
# View package docs
go doc fmt
# View function docs
go doc fmt.Printf
# View all docs
go doc -all fmt
go version
go version # Show Go version
go env
go env # Show all environment variables
go env GOPATH # Show specific variable
go env -w KEY=value # Set environment variable
go clean
go clean # Remove build artifacts
go clean -cache # Clear build cache
go clean -modcache # Clear module cache
go list
go list ./... # List all packages
go list -m all # List all modules
go list -f '{{.ImportPath}}' ./... # Custom format
go fix
go fix ./... # Fix deprecated code
go generate
go generate ./... # Run code generators
go tool
go tool pprof # Performance profiler
go tool cover # Coverage tool
go tool compile # Compiler
go tool link # Linker
Build and Run
go build: Compile packagesgo run: Compile and rungo install: Install packagesTesting
go test: Run testsgo test -bench: Run benchmarksgo test -cover: Show coverageDependencies
go mod init: Initialize modulego mod tidy: Clean dependenciesgo get: Add dependenciesgo mod download: Download modulesgo mod verify: Verify checksumsCode Quality
go fmt: Format codego vet: Check codego doc: View documentationTools
go version: Show versiongo env: Environment variablesgo clean: Clean artifactsgo list: List packagesgo fix: Fix deprecated codego generate: Run generatorsProfiling and Debugging
go test -race: Race detectorgo tool pprof: Profilergo tool cover: CoverageMastering these commands is essential for productive Go development. Each command serves a specific purpose in the Go development workflow.