A.1 Go Command Reference

This section provides a comprehensive reference for the most commonly used Go commands. Understanding these commands is essential for effective Go development.

go build

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

Why 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

go run

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

Why 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

go test

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

Why 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

go mod

Module maintenance commands.

go mod init

go mod init example.com/project  # Initialize new module

Why go mod init

go 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

go get

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

Why 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

go install

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

go fmt

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

go vet

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 ./...

go doc

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

Other Important Commands

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

Complete Command Reference

Build and Run

Testing

Dependencies

Code Quality

Tools

Profiling and Debugging

Mastering these commands is essential for productive Go development. Each command serves a specific purpose in the Go development workflow.