🗓️ 30102024 1445
Core Concept: Essential Go CLI commands for development, testing, building, and dependency management.
Module Management
Initialize New Module
go mod init <module-name>- Create new go.mod filego mod init github.com/user/project- With full import path
Dependency Operations
go get <package>- Add dependency to projectgo get <package>@latest- Get latest versiongo get <package>@v1.2.3- Get specific versiongo get -u- Update all dependenciesgo get -u <package>- Update specific packagego mod download- Download dependencies to cachego mod tidy- Add missing, remove unused dependenciesgo mod verify- Verify dependencies haven't been modified
Module Information
go list -m all- List all dependenciesgo list -m -versions <package>- List available versionsgo mod graph- Print module dependency graphgo mod why <package>- Explain why package is needed
Running Code
Execute
go run main.go- Compile and run single filego run .- Run package in current directorygo run cmd/api/main.go- Run specific file path
Build
go build- Compile package in current directorygo build -o binary-name- Specify output binary namego build cmd/api/main.go- Build specific filego build ./...- Build all packages recursively
Install
go install- Compile and install to $GOPATH/bingo install <package>@latest- Install tool globally
Testing
Run Tests
go test- Run tests in current packagego test ./...- Run all tests recursivelygo test -v- Verbose outputgo test -run TestName- Run specific testgo test -run TestName/SubtestName- Run specific subtest
Test Coverage
go test -cover- Show coverage percentagego test -coverprofile=coverage.out- Generate coverage filego tool cover -html=coverage.out- View coverage in browsergo test -covermode=count- Track how many times lines run
Benchmarks
go test -bench .- Run all benchmarksgo test -bench BenchmarkName- Run specific benchmarkgo test -benchmem- Include memory allocation statsgo test -benchtime=10s- Run for specific duration
Test Options
go test -short- Skip long-running testsgo test -parallel 4- Set parallelism levelgo test -timeout 30s- Set timeoutgo test -race- Enable race detector
Code Quality
Formatting
go fmt- Format code in current packagego fmt ./...- Format all packages recursivelygofmt -w file.go- Format and write specific filegofmt -d .- Show formatting diff without changing
Linting
go vet- Run static analysisgo vet ./...- Vet all packagesgolint ./...- Run golint (install separately)staticcheck ./...- Run staticcheck (install separately)
Race Detection
go run -race main.go- Run with race detectorgo test -race ./...- Test with race detectorgo build -race- Build with race detector
Documentation
View Docs
go doc <package>- Show package documentationgo doc <package>.<Symbol>- Show symbol documentationgo doc -all <package>- Show all documentationgodoc -http=:6060- Start local doc server
Workspace Management
Clean Cache
go clean- Remove object filesgo clean -cache- Remove build cachego clean -modcache- Remove module cachego clean -testcache- Remove test cache
Environment
go env- Print all Go environment variablesgo env GOPATH- Print specific variablego env -w GOPATH=/path- Set environment variablego version- Show Go version
Module Cache Location
Default Paths
- Modules:
~/go/pkg/mod/ - Binaries:
~/go/bin/ - Build cache:
~/Library/Caches/go-build/(macOS)
Common Workflows
Starting New Project
mkdir project && cd projectgo mod init github.com/user/project- Create main.go
go mod tidygo run .
Adding Dependency
go get github.com/package/name- Import in code
go mod tidy
Before Committing
go fmt ./...go vet ./...go test ./...go mod tidy
Building for Production
go test ./...go build -o app- Test binary:
./app
Updating Dependencies
go get -u ./...go mod tidygo test ./...
Build Flags
Common Flags
-o name- Output binary name-v- Verbose output-race- Enable race detector-ldflags "-X main.version=1.0"- Set variables at build time-tags tag1,tag2- Build tags
Cross-Compilation
GOOS=linux GOARCH=amd64 go build- Build for LinuxGOOS=windows GOARCH=amd64 go build- Build for WindowsGOOS=darwin GOARCH=arm64 go build- Build for macOS ARM
Debugging
Print Build Info
go version -m binary- Show build info of binarygo list -f '{{.Deps}}' .- List dependenciesgo list -json .- Package info as JSON
Troubleshooting
go clean -modcache- Clear module cache if corruptedgo mod download- Re-download dependenciesgo mod verify- Check dependency integrityrm go.sum && go mod tidy- Regenerate go.sum
Performance
Profiling
go test -cpuprofile=cpu.out- CPU profilego test -memprofile=mem.out- Memory profilego tool pprof cpu.out- Analyze profilego tool pprof -http=:8080 cpu.out- View in browser
Compilation Speed
go build -a- Force rebuild all packagesgo build -n- Print commands without executinggo build -x- Print commands while executing
Tool Installation
Popular Tools
go install golang.org/x/tools/cmd/godoc@latest- Documentationgo install golang.org/x/lint/golint@latest- Lintergo install honnef.co/go/tools/cmd/staticcheck@latest- Static analysisgo install github.com/golangci/golangci-lint/cmd/golangci-lint@latest- Meta-linter
Git Integration
Files to Commit
- ✅
go.mod- Always commit - ✅
go.sum- Always commit - ❌ Binaries - Add to .gitignore
- ❌
vendor/- Usually ignore (unless vendoring)
.gitignore for Go
# Binaries
*.exe
*.exe~
*.dll
*.so
*.dylib
/bin/
# Test coverage
*.out
coverage.html
# Go workspace
go.work
go.work.sum
# Vendor (if not vendoring)
vendor/
Quick Reference
| Task | Command |
|---|---|
| Start project | go mod init name |
| Add dependency | go get package |
| Run code | go run . |
| Build binary | go build -o app |
| Run tests | go test ./... |
| Format code | go fmt ./... |
| Check code | go vet ./... |
| Clean deps | go mod tidy |
| Update deps | go get -u ./... |
| Show coverage | go test -cover ./... |
Related Concepts
- go_modules - Module system details
- go_testing - Testing patterns
- golang_basics - Language fundamentals
- go_learning_plan - Structured learning path