Go Modules Cheat Sheet
Explains Go modules: initializing go.mod, adding and upgrading dependencies with go get, go mod tidy, and semantic versioning rules.
1 PageBeginnerApr 8, 2026
Initializing a Module
Creating go.mod and building the module.
bash
go mod init github.com/username/myproject # Create go.mod, declares the module pathgo build ./... # Build all packages in the modulego run main.go # Compile and run
Managing Dependencies
Adding, upgrading, and cleaning up dependencies.
bash
go get github.com/gin-gonic/gin@v1.9.1 # Add a dependency at a specific versiongo get -u ./... # Upgrade all dependencies to latest minor/patchgo mod tidy # Add missing / remove unused dependenciesgo list -m all # List the module and all its dependencies
go.mod File
The declaration file for a module and its requirements.
yaml
module github.com/username/myprojectgo 1.22require ( github.com/gin-gonic/gin v1.9.1 github.com/stretchr/testify v1.8.4)require ( // indirect dependencies pulled in transitively github.com/bytedance/sonic v1.9.1 // indirect)
Concepts
How Go's module system resolves and verifies dependencies.
- go.mod- Declares the module path, Go version, and direct/indirect dependencies
- go.sum- Records cryptographic checksums of dependencies to ensure reproducible, verified builds
- Semantic versioning- Modules are versioned vX.Y.Z; major version 2+ requires a /vN suffix on the import path
- Minimal version selection- Go picks the minimum version satisfying all module requirements, not always the latest
- replace directive- replace old/module => ../local/path swaps a dependency for local development
- go mod tidy- Reconciles go.mod/go.sum with what the code actually imports
- GOPROXY- Environment variable controlling the module proxy used to fetch dependencies (default proxy.golang.org)
Pro Tip
Run go mod tidy before every commit that changes imports — it keeps go.mod and go.sum in sync and catches unused or missing dependencies that would otherwise fail a clean CI build.
Was this cheat sheet helpful?
Explore Topics
#GoModules#GoModulesCheatSheet#Programming#Beginner#InitializingAModule#ManagingDependencies#GoModFile#Concepts#CheatSheet#SkillVeris
Advertisement
Sri Hayavadhana Info-Tech
Professional Web Designing Services
- Responsive Websites
- E-commerce Solutions
- SEO Friendly Design
- Fast & Secure
- Support & Maintenance