Go modules are Go's dependency management system, introduced in Go 1.11. They provide official support for versioning and dependency resolution, making it easy to manage external dependencies in your projects.
What Are Go Modules?
A Go module is a collection of Go packages that have a common go.mod file.
Module Definition
The go.mod file defines the module path and dependencies:
module github.com/user/myproject
go 1.21
require (
github.com/gorilla/mux v1.8.0
github.com/stretchr/testify v1.8.4
)Benefits
- Official dependency management
- Semantic versioning support
- Reproducible builds
- No need for GOPATH
- Direct and transitive dependency versioning
Creating a Module
Initialize a new module with go mod init.
Basic Initialization
mkdir myproject
cd myproject
go mod init github.com/user/myprojectThis creates go.mod:
module github.com/user/myproject
go 1.21Module Path
The module path is typically the repository location:
# For GitHub
go mod init github.com/username/myproject
# For GitLab
go mod init gitlab.com/username/myproject
# For local development
go mod init myprojectAdding Dependencies
Go automatically adds dependencies when you import them.
Automatic Addition
package main
import "github.com/gorilla/mux"
func main() {
r := mux.NewRouter()
}Run go build or go mod tidy:
go buildGo updates go.mod and go.sum automatically.
Manual Addition
go get github.com/gorilla/mux@latest
go get github.com/gorilla/mux@v1.8.0Specific Versions
# Specific version
go get github.com/pkg/errors@v0.9.1
# Branch or commit
go get github.com/pkg/errors@master
go get github.com/pkg/errors@abc123defgo.mod File Structure
Understanding go.mod is crucial for dependency management.
Module Declaration
module github.com/user/projectDefines the module's import path.
Go Version
go 1.21Minimum Go version required.
Require Directive
require (
github.com/gorilla/mux v1.8.0
github.com/stretchr/testify v1.8.4
)Lists direct dependencies.
Indirect Dependencies
require github.com/davecgh/go-spew v1.1.1 // indirectTransitive dependencies not directly imported.
Exclude Directive
exclude github.com/bad/module v1.0.0Excludes specific problematic versions.
Replace Directive
replace github.com/old/module => github.com/new/module v1.0.0Replaces a module with another.
Local Replace
replace github.com/user/project => ../projectUseful for local development.
go.sum File
go.sum contains cryptographic checksums for dependency verification.
Purpose
github.com/gorilla/mux v1.8.0 h1:x...
github.com/gorilla/mux v1.8.0/go.mod h1:...Ensures reproducible builds and security.
Don't Edit Manually
go.sum is maintained by Go tooling.
Managing Dependencies
Commands for managing your dependencies.
go mod tidy
Clean up dependencies:
go mod tidy- Adds missing dependencies
- Removes unused dependencies
- Updates go.mod and go.sum
go mod verify
Verify dependencies:
go mod verifyChecks that dependencies haven't been modified.
go mod download
Download dependencies:
go mod downloadDownloads all dependencies to module cache.
go mod why
Explain why a dependency is needed:
go mod why github.com/pkg/errorsShows dependency chain.
go list
List dependencies:
# All dependencies
go list -m all
# Specific module
go list -m github.com/gorilla/muxVersioning
Go modules use semantic versioning.
Version Format
Versions follow semantic versioning: vX.Y.Z
- Major (X): Incompatible API changes
- Minor (Y): New features, backwards compatible
- Patch (Z): Bug fixes, backwards compatible
Pre-release Versions
go get github.com/user/module@v1.0.0-beta
go get github.com/user/module@v1.0.0-rc.1Pseudo-Versions
For untagged commits:
v1.0.0-20230101234567-abc123defUpdating Dependencies
Keep dependencies up-to-date.
Update All
go get -u ./...
go mod tidyUpdates to latest minor/patch versions.
Update Specific
go get -u github.com/gorilla/muxPatch Updates Only
go get -u=patch github.com/gorilla/muxWon't update to new minor/major versions.
Major Version Upgrades
go get github.com/user/module@v2Note: Major version changes often require path changes.
Major Versions
Go modules use module path for major versions.
Major Version 2+
module github.com/user/module/v2
require github.com/user/module/v2 v2.0.0The /v2 suffix is required.
Importing Major Versions
import "github.com/user/module/v2"Multiple Major Versions
require (
github.com/user/module v1.5.0
github.com/user/module/v2 v2.0.0
)Vendoring
Vendor dependencies in your project.
Vendor Dependencies
go mod vendorCreates vendor/ directory with all dependencies.
Use Vendor
go build -mod=vendorBuilds from vendor directory.
Private Repositories
Use private Git repositories as modules.
Authentication
Configure Git credentials:
git config --global url."https://username:token@github.com/".insteadOf "https://github.com/"Or use SSH:
git config --global url."git@github.com:".insteadOf "https://github.com/"GOPRIVATE Environment
export GOPRIVATE=github.com/mycompany/*Bypasses proxy and checksum database for private repos.
Module Proxy
Go uses a public module proxy by default.
Default Proxy
Checksum Database
Verifies module integrity.
Custom Proxy
export GOPROXY=https://goproxy.io,direct
export GOPROXY=https://goproxy.cn,directDirect Mode
export GOPROXY=directBypasses proxy, connects directly.
Workspace Mode
Work with multiple modules together.
go.work File
go work init
go work use ./module1
go work use ./module2Creates go.work:
go 1.21
use (
./module1
./module2
)Benefits
- Edit multiple modules simultaneously
- Replace with local directories automatically
- Test changes across modules
Common Patterns
Minimal go.mod
module github.com/user/project
go 1.21Let Go manage dependencies automatically.
Development Replace
module github.com/user/project
go 1.21
replace github.com/user/project => ../projectUse local version during development.
Testing Forks
replace github.com/original/repo => github.com/fork/repo v1.0.0Test your fork before contributing.
Best Practices
- 1Always commit go.mod and go.sum
- 2Use semantic versioning
- 3Keep dependencies updated
- 4Run go mod tidy regularly
- 5Review changes before committing
- 6Use workspace mode for multi-module projects
- 7Pin critical dependencies
- 8Use GOPRIVATE for private repos
Common Issues
Dependency Conflicts
go: github.com/conflicting/module@v1.0.0 requires
github.com/other/module@v2.0.0Solution: Use replace directive.
Import Version Mismatch
import "github.com/user/module" // Uses v1When go.mod specifies v2:
import "github.com/user/module/v2" // CorrectOutdated go.sum
go mod verify
go mod downloadDebugging Modules
View Dependency Graph
go mod graph | grep github.com/user/moduleCheck Module Info
go mod edit -require github.com/user/module@v1.0.0
go list -m -json github.com/user/moduleFix Corrupted go.sum
go mod download
go mod verifyPerformance Tips
Module Cache
Go caches modules locally:
# Clear cache
go clean -modcacheParallel Downloads
Downloads are parallel by default.
Conclusion
Go modules provide a robust, official solution for dependency management in Go. They combine simplicity with powerful features like semantic versioning, reproducible builds, and workspace mode.
Master Go modules, and you'll manage dependencies confidently, whether working on small projects or large multi-module codebases. The tooling is designed to get out of your way while ensuring your builds are reproducible and secure.
Practice with go mod commands, understand semantic versioning, and leverage workspace mode for complex projects. Go modules are an essential part of modern Go development.