- Published on
Mastering the Go Command - Complete Guide
- Authors
- Name
- Muhammad Yasir
Mastering the Go Command - Complete Guide
The go
command is the Swiss Army knife of Go development. It's your gateway to building, testing, formatting, and managing Go programs. Understanding its capabilities is essential for efficient Go development. This guide covers everything from basic usage to advanced techniques used in enterprise environments.
Overview of the Go Command
The go
command is a command-line tool that provides all necessary functionality for Go development:
# Basic syntax
go <command> [arguments]
# Get help
go help
go help <command>
Core Philosophy
- Convention over Configuration: Minimal configuration required
- Zero Dependencies: Self-contained toolchain
- Fast Execution: Optimized for developer productivity
- Cross-Platform: Works identically across operating systems
Essential Go Commands
go version
# Check Go version
go version
# Output: go version go1.21.5 linux/amd64
# Detailed version info
go version -m /path/to/binary
go env
# Show all environment variables
go env
# Show specific variable
go env GOPATH
go env GOOS
go env GOARCH
# Set environment variable
go env -w GOPROXY=direct
go env -w GO111MODULE=on
# Unset environment variable
go env -u GOPROXY
Key Environment Variables:
GOROOT=/usr/local/go # Go installation
GOPATH=/home/user/go # Workspace (legacy)
GOOS=linux # Target OS
GOARCH=amd64 # Target architecture
GO111MODULE=on # Module mode
GOPROXY=proxy.golang.org # Module proxy
GOSUMDB=sum.golang.org # Checksum database
Building and Running
go run
Execute Go programs directly without creating binaries:
# Run single file
go run main.go
# Run multiple files
go run main.go utils.go
# Run with arguments
go run main.go arg1 arg2
# Run package
go run .
go run ./cmd/server
# Run with build flags
go run -ldflags="-X main.version=1.0.0" main.go
# Run with race detection
go run -race main.go
Advanced Usage:
# Set environment variables
GOOS=windows go run main.go
# Enable debugging
go run -gcflags="-N -l" main.go
# Verbose output
go run -x main.go
go build
Compile Go programs into executable binaries:
# Build current package
go build
# Build specific file
go build main.go
# Build with custom output name
go build -o myapp main.go
# Build for different OS/architecture
GOOS=linux GOARCH=amd64 go build
GOOS=windows GOARCH=amd64 go build -o app.exe
# Build all packages
go build ./...
Build Optimization:
# Reduce binary size
go build -ldflags="-s -w" main.go
# Static linking
CGO_ENABLED=0 go build -a -ldflags '-extldflags "-static"' .
# Debug build
go build -gcflags="-N -l" main.go
# Release build with version
go build -ldflags="-X main.Version=1.0.0 -X main.BuildTime=$(date)" main.go
go install
Compile and install packages:
# Install current package
go install
# Install specific package
go install github.com/user/package@latest
go install github.com/user/package@v1.2.3
# Install to specific location
GOBIN=/usr/local/bin go install
# Install all tools
go install ./cmd/...
Example: Installing Popular Tools
# Install golangci-lint
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
# Install air (live reload)
go install github.com/cosmtrek/air@latest
# Install delve debugger
go install github.com/go-delve/delve/cmd/dlv@latest
Testing Commands
go test
Run tests and benchmarks:
# Run tests in current package
go test
# Run tests in all packages
go test ./...
# Verbose output
go test -v
# Run specific test
go test -run TestFunctionName
go test -run "TestUser.*"
# Run tests with coverage
go test -cover
go test -coverprofile=coverage.out
# Generate HTML coverage report
go test -coverprofile=coverage.out
go tool cover -html=coverage.out -o coverage.html
# Run benchmarks
go test -bench=.
go test -bench=BenchmarkFunction
# Race detection
go test -race
# Short tests only
go test -short
# Parallel execution
go test -parallel 4
Advanced Testing:
# Test with build tags
go test -tags=integration
# Memory profiling
go test -memprofile=mem.prof
# CPU profiling
go test -cpuprofile=cpu.prof
# Timeout
go test -timeout=30s
# Fail fast
go test -failfast
# JSON output
go test -json
Test Example Project Structure
project/
├── main.go
├── main_test.go
├── internal/
│ ├── user/
│ │ ├── user.go
│ │ └── user_test.go
│ └── auth/
│ ├── auth.go
│ └── auth_test.go
└── integration/
└── api_test.go
Module Management
go mod
Manage Go modules and dependencies:
# Initialize new module
go mod init github.com/user/project
# Add dependencies
go get github.com/gorilla/mux
go get github.com/gorilla/mux@v1.8.0
# Add development dependencies
go get -t github.com/stretchr/testify
# Update dependencies
go get -u
go get -u ./...
# Remove unused dependencies
go mod tidy
# Download dependencies
go mod download
# Verify dependencies
go mod verify
# Show module graph
go mod graph
# Show why dependency is needed
go mod why github.com/gorilla/mux
# Edit go.mod
go mod edit -require=github.com/user/package@v1.0.0
go mod edit -exclude=github.com/bad/package@v1.0.0
go mod edit -replace=github.com/old/package=github.com/new/package@v1.0.0
Module Commands in Practice:
# Create new project
mkdir myproject && cd myproject
go mod init github.com/username/myproject
# Add dependencies
go get github.com/gin-gonic/gin
go get github.com/lib/pq
# Update to latest
go get -u github.com/gin-gonic/gin
# Clean up
go mod tidy
# Vendor dependencies (optional)
go mod vendor
go.mod File Structure
module github.com/username/myproject
go 1.21
require (
github.com/gin-gonic/gin v1.9.1
github.com/lib/pq v1.10.9
)
require (
github.com/bytedance/sonic v1.9.1 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
// ... more indirect dependencies
)
exclude github.com/bad/package v1.0.0
replace github.com/old/package => github.com/new/package v1.0.0
retract v1.0.1 // Published accidentally
Code Quality and Formatting
go fmt
Format Go source code:
# Format current directory
go fmt
# Format specific file
go fmt main.go
# Format all packages
go fmt ./...
# Show differences without formatting
gofmt -d .
# Format and write
gofmt -w .
go vet
Static analysis tool:
# Vet current package
go vet
# Vet all packages
go vet ./...
# Specific checks
go vet -printf=false ./...
go vet -structtag=false ./...
# Show all available analyzers
go tool vet help
Common Issues go vet Catches:
- Printf format string mismatches
- Unreachable code
- Suspicious constructs
- Struct tag validation
- Assignment to nil maps
goimports
Manage imports automatically:
# Install goimports
go install golang.org/x/tools/cmd/goimports@latest
# Format and fix imports
goimports -w .
# Show differences
goimports -d .
Documentation
go doc
View documentation:
# Package documentation
go doc fmt
go doc net/http
# Function documentation
go doc fmt.Printf
go doc net/http.ListenAndServe
# Method documentation
go doc json.Decoder.Decode
# Show all exported items
go doc -all fmt
# Show source code
go doc -src fmt.Printf
godoc
Generate and serve documentation:
# Install godoc
go install golang.org/x/tools/cmd/godoc@latest
# Serve documentation locally
godoc -http=:6060
# Generate static documentation
godoc -html fmt > fmt.html
Advanced Commands
go list
List packages and modules:
# List current package
go list
# List all packages
go list ./...
# List with templates
go list -f '{{.ImportPath}} {{.Dir}}' ./...
# List dependencies
go list -deps
# List test files
go list -f '{{.TestGoFiles}}' ./...
# List modules
go list -m all
go list -m -versions github.com/gorilla/mux
# JSON output
go list -json
go work
Workspace management (Go 1.18+):
# Initialize workspace
go work init ./module1 ./module2
# Add module to workspace
go work use ./module3
# Edit workspace
go work edit -use=./module4
# Sync workspace
go work sync
Workspace Example:
workspace/
├── go.work
├── service-a/
│ ├── go.mod
│ └── main.go
├── service-b/
│ ├── go.mod
│ └── main.go
└── shared/
├── go.mod
└── utils.go
go generate
Run code generation tools:
# Generate code
go generate
# Generate for all packages
go generate ./...
# Verbose output
go generate -v
Example generation directive:
//go:generate stringer -type=Status
//go:generate mockgen -source=interface.go -destination=mock.go
type Status int
const (
Pending Status = iota
Running
Complete
)
Performance and Profiling
go tool pprof
Performance profiling:
# CPU profiling
go test -cpuprofile=cpu.prof -bench=.
go tool pprof cpu.prof
# Memory profiling
go test -memprofile=mem.prof -bench=.
go tool pprof mem.prof
# Web interface
go tool pprof -http=:8080 cpu.prof
# Top functions
go tool pprof -top cpu.prof
# List source code
go tool pprof -list=main.* cpu.prof
go tool trace
Execution tracing:
# Generate trace
go test -trace=trace.out
# View trace
go tool trace trace.out
go tool compile
Compiler tools:
# Escape analysis
go build -gcflags="-m" main.go
# Assembly output
go tool compile -S main.go
# Optimization levels
go build -gcflags="-l" main.go # Disable inlining
go build -gcflags="-N" main.go # Disable optimizations
Build Constraints and Tags
Build Tags
//go:build linux
// +build linux
package main
// This file only builds on Linux
# Build with tags
go build -tags="debug,integration"
# List build constraints
go list -f '{{.GoFiles}} {{.TestGoFiles}}' -tags=debug
Common Build Tags
//go:build !windows
//go:build linux || darwin
//go:build cgo
//go:build debug
//go:build integration
Enterprise Use Cases
CI/CD Pipeline Script
#!/bin/bash
set -e
# Linting
echo "Running linters..."
golangci-lint run
# Testing
echo "Running tests..."
go test -race -coverprofile=coverage.out ./...
# Coverage check
coverage=$(go tool cover -func=coverage.out | tail -1 | awk '{print $3}' | sed 's/%//')
if (( $(echo "$coverage < 80" | bc -l) )); then
echo "Coverage $coverage% is below 80%"
exit 1
fi
# Security scan
echo "Running security scan..."
gosec ./...
# Build
echo "Building application..."
go build -ldflags="-s -w -X main.Version=$VERSION" -o app
echo "Build successful!"
Makefile for Go Projects
.PHONY: build test clean lint cover deps
# Build configuration
APP_NAME := myapp
VERSION := $(shell git describe --tags --always --dirty)
BUILD_TIME := $(shell date -u '+%Y-%m-%d_%H:%M:%S')
LDFLAGS := -s -w -X main.Version=$(VERSION) -X main.BuildTime=$(BUILD_TIME)
# Build targets
build:
go build -ldflags="$(LDFLAGS)" -o bin/$(APP_NAME) ./cmd/$(APP_NAME)
build-all:
GOOS=linux GOARCH=amd64 go build -ldflags="$(LDFLAGS)" -o bin/$(APP_NAME)-linux-amd64 ./cmd/$(APP_NAME)
GOOS=windows GOARCH=amd64 go build -ldflags="$(LDFLAGS)" -o bin/$(APP_NAME)-windows-amd64.exe ./cmd/$(APP_NAME)
GOOS=darwin GOARCH=amd64 go build -ldflags="$(LDFLAGS)" -o bin/$(APP_NAME)-darwin-amd64 ./cmd/$(APP_NAME)
# Testing
test:
go test -v -race ./...
test-cover:
go test -v -race -coverprofile=coverage.out ./...
go tool cover -html=coverage.out -o coverage.html
bench:
go test -bench=. -benchmem ./...
# Code quality
lint:
golangci-lint run
fmt:
gofmt -s -w .
goimports -w .
vet:
go vet ./...
# Dependencies
deps:
go mod download
go mod tidy
deps-update:
go get -u ./...
go mod tidy
# Utilities
clean:
rm -rf bin/
rm -f coverage.out coverage.html
install:
go install ./cmd/$(APP_NAME)
run:
go run ./cmd/$(APP_NAME)
Troubleshooting Common Issues
Module Issues
# Clear module cache
go clean -modcache
# Force re-download
go mod download -x
# Fix go.sum mismatches
go mod tidy
Build Issues
# Clean build cache
go clean -cache
# Rebuild everything
go build -a ./...
# Debug build process
go build -x ./...
Import Issues
# Fix imports
goimports -w .
# Check import paths
go list -f '{{.ImportPath}} -> {{.Dir}}' ./...
Performance Tips
Build Performance
# Parallel builds
GOMAXPROCS=8 go build ./...
# Use build cache
export GOCACHE=/tmp/go-cache
# Disable CGO for faster builds
CGO_ENABLED=0 go build
Test Performance
# Parallel testing
go test -parallel 8 ./...
# Cache test results
go test -cache ./...
# Short tests in development
go test -short ./...
Interview Questions
Basic Commands
What's the difference between
go build
andgo install
?Answer: Key differences in purpose and behavior:
go build
:- Output Location: Creates executable in current directory
- Purpose: Compile for immediate use or testing
- Behavior:
go build main.go
createsmain
executable locally - Use Case: Development and local testing
go install
:- Output Location: Installs to
$GOPATH/bin
or$GOBIN
- Purpose: Install tools and binaries for system-wide use
- Behavior:
go install github.com/user/tool@latest
installs to PATH - Use Case: Installing development tools and utilities
# Build locally go build -o myapp main.go # Install system-wide go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
How do you run tests with coverage and what does it tell you?
Answer: Coverage shows which lines of code are executed during tests:
Basic Coverage:
# Simple coverage percentage go test -cover # Output: coverage: 85.7% of statements # Detailed coverage report go test -coverprofile=coverage.out go tool cover -html=coverage.out -o coverage.html
What Coverage Tells You:
- Statement Coverage: Percentage of lines executed
- Branch Coverage: Which conditional paths are tested
- Function Coverage: Which functions are called
- Uncovered Code: Lines that need more tests
Coverage Best Practices:
- Target 80%+ coverage for critical code
- 100% coverage isn't always necessary
- Focus on testing business logic
- Use coverage to find missed edge cases
What does
go mod tidy
do and when should you use it?Answer:
go mod tidy
maintains clean dependency management:Functions:
- Remove Unused: Removes dependencies not referenced in code
- Add Missing: Adds dependencies for imported packages
- Update go.sum: Ensures checksums are current
- Clean Versions: Removes unused versions from go.mod
When to Use:
# After adding new imports import "github.com/new/package" go mod tidy # After removing code/imports go mod tidy # Before committing changes go mod tidy # Regular maintenance go mod tidy
Example Effect:
// Before go mod tidy require ( github.com/used/package v1.0.0 github.com/unused/package v1.0.0 // Will be removed ) // After go mod tidy require ( github.com/used/package v1.0.0 )
Explain the purpose of
go env
and its most important variablesAnswer:
go env
manages Go's configuration environment:Key Variables:
go env GOROOT # Go installation directory go env GOPATH # Workspace directory (legacy) go env GOOS # Target operating system go env GOARCH # Target architecture go env GO111MODULE # Module mode (on/off/auto) go env GOPROXY # Module proxy URL go env GOSUMDB # Checksum database
Setting Variables:
# Set permanently go env -w GOPROXY=direct go env -w GO111MODULE=on # Set for single command GOOS=windows go build main.go # Unset variable go env -u GOPROXY
Common Use Cases:
- Cross-compilation:
GOOS=linux GOARCH=amd64
- Private modules:
GOPROXY=direct
- Debugging:
go env
to check configuration
- Cross-compilation:
Intermediate Questions
How do you cross-compile Go programs and what are the considerations?
Answer: Go supports easy cross-compilation using environment variables:
Basic Cross-compilation:
# Linux binary from any OS GOOS=linux GOARCH=amd64 go build -o myapp-linux main.go # Windows binary GOOS=windows GOARCH=amd64 go build -o myapp.exe main.go # macOS binary GOOS=darwin GOARCH=amd64 go build -o myapp-mac main.go # ARM64 (Apple Silicon, newer ARM servers) GOOS=linux GOARCH=arm64 go build -o myapp-arm64 main.go
Considerations:
- CGO: Disable for easy cross-compilation (
CGO_ENABLED=0
) - File Paths: Use
filepath.Join()
for OS-specific paths - System Calls: Some packages may not work on all platforms
- Testing: Test on target platforms when possible
Makefile Example:
build-all: GOOS=linux GOARCH=amd64 go build -o bin/app-linux GOOS=windows GOARCH=amd64 go build -o bin/app.exe GOOS=darwin GOARCH=amd64 go build -o bin/app-mac
- CGO: Disable for easy cross-compilation (
What build flags would you use for production deployments?
Answer: Production builds require optimization and security considerations:
Size Optimization:
# Strip debug info and symbol table go build -ldflags="-s -w" main.go # Static linking for containers CGO_ENABLED=0 go build -a -ldflags '-extldflags "-static"' main.go
Version Information:
# Inject build-time variables go build -ldflags="-X main.Version=v1.2.3 -X main.BuildTime=$(date -u +%Y%m%d-%H%M%S)" main.go
Security:
# Disable debug info (security) go build -ldflags="-s -w" main.go # Position Independent Executable go build -buildmode=pie main.go
Complete Production Build:
CGO_ENABLED=0 GOOS=linux go build \ -a -installsuffix cgo \ -ldflags="-s -w -X main.Version=${VERSION}" \ -o app main.go
How do you profile a Go application and interpret the results?
Answer: Go provides comprehensive profiling tools:
CPU Profiling:
# Generate CPU profile during test go test -cpuprofile=cpu.prof -bench=. # Analyze profile go tool pprof cpu.prof # Web interface go tool pprof -http=:8080 cpu.prof
Memory Profiling:
# Memory allocation profile go test -memprofile=mem.prof -bench=. # Heap profile of running program go tool pprof http://localhost:6060/debug/pprof/heap
Interpreting Results:
(pprof) top10 Showing nodes accounting for 2.48s, 89.21% of 2.78s total flat flat% sum% cum cum% 0.91s 32.73% 32.73% 0.91s 32.73% runtime.memmove 0.33s 11.87% 44.60% 1.24s 44.60% main.processData
Key Metrics:
- flat: Time spent in function itself
- cum: Time spent in function + called functions
- %: Percentage of total execution time
Explain Go modules and dependency management best practices
Answer: Go modules provide modern dependency management:
Module Structure:
// go.mod module github.com/user/project go 1.21 require ( github.com/gin-gonic/gin v1.9.1 github.com/lib/pq v1.10.9 ) require ( // Indirect dependencies github.com/bytedance/sonic v1.9.1 // indirect )
Best Practices:
# Pin major versions go get github.com/package@v1 # Update dependencies carefully go get -u=patch ./... # Patch updates only go get -u ./... # All updates # Verify dependencies go mod verify # Vendor for reproducible builds go mod vendor
Version Management:
- Semantic Versioning: Use proper semver tags
- Minimum Version Selection: Go picks minimum required versions
- Replace Directives: Override dependencies for testing
- Exclude Directives: Block problematic versions
Advanced Questions
Explain Go's module proxy and checksum database security model
Answer: Go's module system includes security and reliability features:
Module Proxy (GOPROXY):
- Default:
proxy.golang.org,direct
- Caching: Immutable module versions
- Availability: Redundancy for deleted repositories
- Performance: Faster downloads than direct VCS
Checksum Database (GOSUMDB):
- Purpose: Verify module integrity
- Transparency Log: Public, append-only log of checksums
- Verification: Prevents tampering and supply chain attacks
- Trust: Cryptographically verifiable
Security Features:
# Private repositories GOPRIVATE=github.com/company/* go mod download # Direct access (bypass proxy) GOPROXY=direct go mod download # Verify all dependencies go mod verify
Enterprise Configuration:
# Company proxy GOPROXY=https://proxy.company.com,https://proxy.golang.org,direct # Private checksum database GOSUMDB=sum.company.com
- Default:
How would you implement custom build constraints and when would you use them?
Answer: Build constraints control compilation based on conditions:
Syntax:
//go:build linux && amd64 // +build linux,amd64 package main
Common Use Cases:
Platform-specific Code:
//go:build windows package main import "syscall" func platformSpecificFunction() { // Windows-specific implementation }
Feature Flags:
//go:build debug package main import "log" func debugLog(msg string) { log.Println("DEBUG:", msg) }
Integration Tests:
//go:build integration package main func TestDatabaseIntegration(t *testing.T) { // Expensive integration tests }
Building with Tags:
# Include debug build go build -tags=debug # Multiple tags go build -tags="debug,integration" # Platform-specific GOOS=linux go build -tags="linux,production"
What's the purpose of the vendor directory and when should you use it?
Answer: Vendor directory provides dependency bundling:
Purpose:
- Reproducible Builds: Exact dependency versions
- Offline Builds: No network required
- Security: Avoid supply chain attacks
- Compliance: Some organizations require vendoring
Creating Vendor Directory:
# Vendor all dependencies go mod vendor # Build using vendor go build -mod=vendor # Verify vendor is up-to-date go mod vendor && git diff --exit-code vendor/
When to Use:
- CI/CD: Ensure build reproducibility
- Air-gapped: Environments without internet
- Compliance: Regulatory requirements
- Large Projects: Reduce external dependencies
When NOT to Use:
- Development: Slows down dependency updates
- Small Projects: Adds unnecessary complexity
- Public Libraries: Users should control dependencies
How would you optimize Go build times for large projects?
Answer: Several strategies to improve build performance:
Build Cache:
# Enable build cache (default in Go 1.10+) export GOCACHE=/path/to/cache # Clear cache if needed go clean -cache
Parallel Builds:
# Use all CPU cores GOMAXPROCS=0 go build # Explicit core count GOMAXPROCS=8 go build
Incremental Builds:
# Build only changed packages go build ./... # Force rebuild everything go build -a ./...
Module Optimization:
# Pre-download dependencies go mod download # Use vendor for CI/CD go mod vendor go build -mod=vendor
CI/CD Optimizations:
# Docker multi-stage build FROM golang:1.21 AS builder COPY go.mod go.sum ./ RUN go mod download COPY . . RUN go build -o app FROM alpine:latest COPY --from=builder /app /app
Troubleshooting and Debugging
How would you debug module resolution issues?
Answer: Systematic approach to module problems:
Diagnostic Commands:
# Check module status go list -m all # Verbose module operations go get -x github.com/package # Why is this dependency included? go mod why github.com/indirect/package # Module dependency graph go mod graph
Common Issues:
Version Conflicts:
# Check which version is selected go list -m github.com/package # Force specific version go get github.com/package@v1.2.3
Private Repositories:
# Configure private modules go env -w GOPRIVATE=github.com/company/* # Use SSH for private repos git config --global url."git@github.com:".insteadOf "https://github.com/"
Cache Issues:
# Clear module cache go clean -modcache # Re-download dependencies go mod download
What steps would you take if
go get
fails?Answer: Systematic troubleshooting approach:
Network Issues:
# Check proxy settings go env GOPROXY # Try direct access GOPROXY=direct go get package # Check connectivity curl -I https://proxy.golang.org
Authentication Issues:
# Private repositories go env -w GOPRIVATE=github.com/company/* # Git authentication git config --global credential.helper store
Version Issues:
# List available versions go list -m -versions github.com/package # Get specific version go get github.com/package@v1.0.0 # Get latest go get github.com/package@latest
Debug Output:
# Verbose output go get -x -v github.com/package # Check go.mod changes git diff go.mod go.sum
How do you optimize Go applications for different deployment environments?
Answer: Environment-specific optimization strategies:
Container Optimization:
# Multi-stage build FROM golang:1.21-alpine AS builder RUN apk add --no-cache git COPY . . RUN CGO_ENABLED=0 go build -ldflags="-s -w" -o app FROM scratch COPY /app /app ENTRYPOINT ["/app"]
Kubernetes Optimization:
# Static binary for minimal containers CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo # Health check endpoint go build -ldflags="-X main.healthCheck=true"
Lambda/Serverless:
# Minimal binary size go build -ldflags="-s -w" -tags="lambda.norpc" # ARM64 for better price/performance GOOS=linux GOARCH=arm64 go build
Performance Tuning:
# Profile-guided optimization (experimental) go build -pgo=profile.pprof # Disable debug info in production go build -ldflags="-s -w"
Best Practices and Architecture
How would you structure a Go project for a team of 10+ developers?
Answer: Large team project organization:
Directory Structure:
project/ ├── cmd/ # Main applications │ ├── api/ │ ├── worker/ │ └── cli/ ├── internal/ # Private packages │ ├── auth/ │ ├── user/ │ └── order/ ├── pkg/ # Public packages │ ├── logger/ │ └── metrics/ ├── api/ # API definitions ├── deployments/ # Deployment configs ├── scripts/ # Build scripts ├── Makefile └── go.mod
Team Practices:
# Pre-commit hooks go mod tidy go fmt ./... go vet ./... golangci-lint run go test ./...
CI/CD Pipeline:
- name: Test run: | go mod verify go test -race -coverprofile=coverage.out ./... go tool cover -func=coverage.out - name: Build run: | go build -ldflags="-s -w" ./cmd/...
What are the security considerations when using the go command in production?
Answer: Security practices for production Go builds:
Supply Chain Security:
# Verify module checksums go mod verify # Use specific versions, not latest go get github.com/package@v1.2.3 # Audit dependencies go list -m all | audit-tool
Build Security:
# Strip debug information go build -ldflags="-s -w" # Reproducible builds go build -trimpath # Static linking for containers CGO_ENABLED=0 go build
Environment Security:
# Private module access export GOPRIVATE=github.com/company/* # Secure proxy configuration export GOPROXY=https://proxy.company.com # Verify checksums export GOSUMDB=sum.company.com
Runtime Security:
# Enable race detection in testing go test -race ./... # Static analysis go vet ./... gosec ./...
Best Practices
Development Workflow
# Daily development
go mod tidy # Clean dependencies
go fmt ./... # Format code
go vet ./... # Static analysis
go test ./... # Run tests
go build # Build application
Production Builds
# Optimized production build
CGO_ENABLED=0 GOOS=linux go build \
-ldflags="-s -w -X main.Version=$VERSION" \
-o app main.go
Code Quality Checks
# Complete quality check
go fmt ./...
go vet ./...
golangci-lint run
go test -race ./...
go mod tidy
Next Steps
Now that you've mastered the go command, you're ready to:
The go
command is your companion throughout your Go journey. From writing your first Hello World to building enterprise applications, these commands will be essential tools in your developer toolkit. Master them, and you'll be well-equipped to handle any Go development challenge.
Remember: Go's toolchain is designed for simplicity and efficiency. The more familiar you become with these commands, the more productive you'll be as a Go developer.