Essential Tools for Go Development
Go provides a rich set of built-in tools and commands that help in development, debugging, and code maintenance. This guide covers essential tools that every Go developer should know.
Core Go Tools
go build
Compiles packages and dependencies:
# Build current package
go build
# Build specific package
go build github.com/username/project
# Cross-compile for different OS/architecture
GOOS=linux GOARCH=amd64 go build
# Build with optimization
go build -ldflags="-s -w"
go run
Compiles and runs Go programs:
# Run current package
go run .
# Run specific file
go run main.go
# Run with arguments
go run main.go arg1 arg2
# Run multiple files
go run file1.go file2.go
go test
Runs tests in Go packages:
# Run all tests
go test ./...
# Run specific test
go test -run TestFunction
# Run with coverage
go test -cover ./...
# Generate coverage report
go test -coverprofile=coverage.out ./...
go tool cover -html=coverage.out
go fmt
Formats Go source code:
# Format current package
go fmt
# Format recursively
go fmt ./...
# Format specific file
go fmt file.go
# Check formatting without modifying
gofmt -d file.go
Code Analysis Tools
go vet
Examines Go source code and reports suspicious constructs:
# Vet current package
go vet
# Vet specific package
go vet github.com/username/project
# Vet with specific checks
go vet -shadow ./...
golangci-lint
Comprehensive linting tool:
# Install
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
# Run default linters
golangci-lint run
# Run specific linters
golangci-lint run --enable=gosimple,govet
# Configure via .golangci.yml
linters:
enable:
- gosimple
- govet
- errcheck
staticcheck
Advanced static analysis:
# Install
go install honnef.co/go/tools/cmd/staticcheck@latest
# Run analysis
staticcheck ./...
# Check specific package
staticcheck github.com/username/project
Documentation Tools
godoc
Documentation generator and viewer:
# Install
go install golang.org/x/tools/cmd/godoc@latest
# Start documentation server
godoc -http=:6060
# View documentation in terminal
go doc fmt.Println
pkgsite
Modern documentation viewer:
# Install
go install golang.org/x/pkgsite/cmd/pkgsite@latest
# Start documentation server
pkgsite -http=:8080
Debugging Tools
delve
Powerful debugger for Go:
# Install
go install github.com/go-delve/delve/cmd/dlv@latest
# Start debugging session
dlv debug
# Attach to running process
dlv attach <pid>
# Debug test
dlv test
# Common commands in debug session
(dlv) break main.go:20
(dlv) continue
(dlv) next
(dlv) step
(dlv) print variable
pprof
Profiling tool for performance analysis:
# CPU profiling
go test -cpuprofile=cpu.prof -bench .
# Memory profiling
go test -memprofile=mem.prof -bench .
# View profile
go tool pprof cpu.prof
# Generate graph
go tool pprof -png cpu.prof > cpu.png
Code Generation Tools
stringer
Generates String() methods for constants:
# Install
go install golang.org/x/tools/cmd/stringer@latest
# Generate
//go:generate stringer -type=Enum
type Enum int
const (
EnumOne Enum = iota
EnumTwo
)
mockgen
Generates mock interfaces for testing:
# Install
go install github.com/golang/mock/mockgen@latest
# Generate mock
mockgen -source=interface.go -destination=mock_interface.go
Development Environment Tools
gopls
Official Go language server:
# Install
go install golang.org/x/tools/gopls@latest
# VS Code settings.json
{
"go.useLanguageServer": true,
"go.languageServerFlags": [],
"[go]": {
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.organizeImports": true
}
}
}
goimports
Import management tool:
# Install
go install golang.org/x/tools/cmd/goimports@latest
# Format and fix imports
goimports -w file.go
# Check without modifying
goimports -d file.go
Database Tools
migrate
Database migration tool:
# Install
go install -tags 'postgres' github.com/golang-migrate/migrate/v4/cmd/migrate@latest
# Create migration
migrate create -ext sql -dir migrations -seq create_users_table
# Run migrations
migrate -database "postgres://localhost:5432/dbname?sslmode=disable" -path migrations up
Performance Tools
benchstat
Statistical analysis of benchmarks:
# Install
go install golang.org/x/perf/cmd/benchstat@latest
# Run benchmark
go test -bench . -count 5 > old.txt
# Make changes
go test -bench . -count 5 > new.txt
# Compare results
benchstat old.txt new.txt
Project Management Tools
air
Live reload for Go applications:
# Install
go install github.com/cosmtrek/air@latest
# Initialize config
air init
# Run with live reload
air
Security Tools
gosec
Security checker for Go code:
# Install
go install github.com/securego/gosec/v2/cmd/gosec@latest
# Run security scan
gosec ./...
# Generate HTML report
gosec -fmt=html -out=security.html ./...
Best Practices
1. Tool Integration
// .vscode/settings.json
{
"go.useLanguageServer": true,
"go.lintTool": "golangci-lint",
"go.formatTool": "goimports",
"go.testOnSave": true,
"go.coverOnSave": true
}
2. Pre-commit Hooks
#!/bin/sh
# .git/hooks/pre-commit
go fmt ./...
go vet ./...
golangci-lint run
go test ./...
3. CI/CD Integration
# .github/workflows/go.yml
name: Go
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-go@v2
with:
go-version: '1.22'
- run: go test ./...
- run: golangci-lint run
Common Issues and Solutions
1. Tool Installation Issues
# Clear module cache
go clean -modcache
# Update all tools
go get -u all
2. Version Conflicts
# Check Go version
go version
# Update Go tools
go install golang.org/x/tools/...@latest
Next Steps
- Set up your development environment
- Learn about Go modules
- Explore testing practices
- Study performance optimization