A Go module is a collection of Go packages stored in a directory with a go.mod file at its root. This file defines the module’s path and its dependencies. It allows developers to version, upgrade, and manage dependencies cleanly.
Key Files
go.mod: Contains the module name, Go version, and required dependencies with their versions.
go.sum: Maintains checksums to verify the integrity of modules used.
Basic Commands
go mod init : Initializes a new module.
go get @: Adds or updates a dependency.
go mod tidy: Cleans up unused dependencies and adds missing ones.
go build / go run / go test: Automatically resolves and uses modules.
Benefits
- No need for GOPATH.
- Clear version control and reproducibility.
- Easier to work across multiple projects.
Example
go mod init github.com/username/myproject go get github.com/gin-gonic/gin@v1.9.0
This will create a go.mod file and fetch the specified version of the Gin web framework.
Go Modules make dependency management in Go modern and efficient, especially important as projects grow in complexity.
Top comments (0)