Go manages dependencies with Go modules, built into the go command since Go 1.11 and the only supported mode today. A module is defined by a go.mod file that lists the module path, the Go version and the required dependency versions, while go.sum records cryptographic hashes that make builds verifiable. The standard library has no package manager of its own; the go tool does the work, and packages such as debug/buildinfo and runtime/debug let a program read its own dependency information.
This article covers the commands, versioning rules and strategies that keep Go dependencies reproducible and safe.
A module is a tree of packages with a go.mod file at its root. Create one with go mod init:
The resulting go.mod looks like this:
Go uses Minimal Version Selection (MVS). Instead of picking the newest compatible release, it picks the minimum version that satisfies every requirement in the dependency graph. Builds are therefore reproducible without a separate lock file, and upgrading is always an explicit action.
Versions follow semantic versioning with a Go-specific rule: a major version of 2 or higher is part of the import path, for example github.com/org/lib/v2. That lets two major versions coexist in one build.
| Command | Purpose |
|---|---|
go get pkg@version | Add or change a dependency |
go get -u ./... | Update dependencies to the latest minor or patch releases |
go mod tidy | Add missing and remove unused requirements, update go.sum |
go mod download | Fill the module cache |
go mod verify | Check cached modules against go.sum |
go mod why -m pkg | Explain why a module is needed |
go mod graph | Print the dependency graph |
go list -m -u all | Show available upgrades |
go list -m all | List every module in the build |
Run go mod tidy before every commit, and commit both go.mod and go.sum. Do not edit go.sum by hand.
Go embeds module information in every binary built in module mode. runtime/debug.ReadBuildInfo returns it at run time, which is handy for a /version endpoint.
From the command line, go version -m ./binary prints the same data for any binary, and govulncheck uses it to find known vulnerabilities in the exact versions you ship.
replace redirects a module to another path or a local directory, useful for a fork or local development: replace example.com/lib => ../lib.exclude blocks a specific version from being selected.go mod vendor copies dependencies into a vendor directory so a build needs no network access. The go command uses it automatically when vendor/modules.txt is consistent with go.mod.go.work workspaces (Go 1.18 and later) let you develop several modules together without editing go.mod files. Create one with go work init ./app ./lib, and do not commit it unless the whole team uses it.tool directive (Go 1.24 and later) records developer tools such as code generators in go.mod, replacing the old blank-import tools.go trick. Add one with go get -tool pkg.By default go downloads through the public module proxy and checks hashes against the checksum database, which protects against tampered or swapped versions. Configure this with environment variables:
GOPRIVATE marks paths that must bypass the proxy and checksum database. For a company, run an internal proxy (a module proxy such as Athens or an artifact repository) to cache modules and keep builds working if an upstream repository disappears.
go.mod and review changes to it in every pull request.go list -m -u all to see what moved.govulncheck ./... in continuous integration.go directive current and use the toolchain line (Go 1.21 and later) so every developer and CI job builds with the same Go version.retract directive, then publish a new version.v1.2.3 and never move or delete a published tag, because the proxy caches it permanently.No separate one. go.mod records the selected minimum versions and go.sum records the hashes, and together they make builds reproducible.
go get changes the dependencies in your go.mod. go install pkg@version builds and installs a command without touching your module.
Only for the module cache and installed binaries. Source code lives in any directory, because module mode is the default and GOPATH mode has been removed from normal use.
Run go get example.com/[email protected], then go mod tidy. Go lowers the requirement to that version.
Go dependency management is handled by modules: a go.mod file, go.sum hashes, Minimal Version Selection and the go mod and go get commands. Keep the files tidy, scan with govulncheck, use GOPRIVATE and a proxy for private code, and update in small, tested steps.