Go modules
Context
Section titled “Context”A Go module is a collection of related Go packages versioned together as a single unit. Modules replace the old GOPATH‑based workflow and provide:
- Versioning – Semantic versions (v1.2.3) are enforced.
- Dependency management –
go.modlists required modules and versions. - Reproducible builds –
go.sumensures integrity.
The module system was introduced in Go 1.11 and became the default in Go 1.16.
Core files
Section titled “Core files”go.mod– Defines the module path (e.g.,github.com/user/project) and its dependencies.go.sum– Contains checksums of dependencies to verify integrity.
Basic commands
Section titled “Basic commands”| Command | Purpose |
|---|---|
go mod init <module> | Create a new module in the current directory. |
go get <module> | Add a dependency (or update it). |
go mod tidy | Remove unused dependencies and add missing ones. |
go mod vendor | Copy dependencies to a vendor/ folder (optional). |
Example
Section titled “Example”Create a new module named “hello”.
Code example
Section titled “Code example”mkdir myprojectcd myprojectgo mod init helloOutput
Section titled “Output”go: creating new go.mod: module helloNow go.mod contains:
module hello
go 1.24Adding a dependency
Section titled “Adding a dependency”go get rsc.io/quoteThis updates go.mod and downloads the module.