Go manages dependencies with modules. A go.mod file at the root of your project lists the module path, the Go version and every required module version, and a go.sum file stores cryptographic checksums. You create the module with go mod init, add dependencies with go get, and keep the file tidy with go mod tidy.
Modules have been the default since Go 1.16, and the old GOPATH workflow is no longer used for new projects. This article walks through the full lifecycle: initialising, adding, upgrading, pinning, removing, vendoring and auditing dependencies.
Run go mod init in your project directory with a module path. For code that others will import, use the repository URL as the path.
This creates go.mod:
The go line states the minimum language version for the module. Since Go 1.21 it is a strict requirement, and a toolchain line can additionally select which Go toolchain builds the module, which the go command downloads automatically when needed.
Import a package in your code, then fetch it. A minimal program using the Gin web framework looks like this:
go get downloads the latest release, adds a require line to go.mod, and records checksums in go.sum. You can also just run go mod tidy, which resolves missing imports for you.
| Command | What it does |
|---|---|
go get pkg | Adds the latest version of a module |
go get [email protected] | Adds or changes to an exact version |
go get pkg@latest | Moves to the newest release |
go get -u ./... | Updates dependencies of your packages to the latest minor or patch releases |
go get -u=patch ./... | Updates to the latest patch releases only |
go list -m -u all | Lists modules that have available updates |
To pin a dependency to a specific version, request it with @:
The version in go.mod is the minimum that the build uses. You can also use @commit-hash or a branch name for unreleased code, though tagged versions are better for reproducibility. To force a replacement, for example while testing a local fork, use the replace directive:
Do not commit local-path replacements into a shared module, since other machines will not have that path.
go mod tidy adds requirements for imports that are missing and removes requirements nothing uses. Run it before every commit that changes imports.
go mod verify checks that downloaded modules in the local cache still match go.sum. Commit both go.mod and go.sum to version control. Do not edit go.sum by hand.
Go uses minimal version selection (MVS). For each module, it picks the highest version required by any module in the build graph, never a newer one than someone asked for. This makes builds reproducible without a separate lock file: the same go.mod always selects the same versions, and nothing upgrades silently.
Major versions are part of the import path from v2 onward. A module at v2 is imported as github.com/example/lib/v2, so two major versions can coexist in one build. Go 1.24 and later also support a tool directive in go.mod, which tracks developer tools such as code generators alongside regular dependencies, used via go get -tool and go tool.
go mod vendor copies all dependencies into a vendor/ directory, and go build -mod=vendor builds from it. This helps with offline builds and strict audit requirements. By default, if the vendor directory exists and is consistent with go.mod, the go command already uses it.
By default Go fetches through the public module proxy proxy.golang.org and checks the sum.golang.org checksum database. For private repositories, set GOPRIVATE:
The official govulncheck tool reports known vulnerabilities that actually affect the code paths you call.
Run it in CI, and also review why a module is present with go mod why -m module/path and go mod graph.
go mod init example.com/myapp once.go mod tidy to add the requirements.go list -m -u all regularly to see updates, then go get -u ./... and run your tests.go.mod and go.sum together.govulncheck ./... in CI.go get changes the version of a specific module. go mod tidy syncs go.mod and go.sum with the imports in your code, adding what is missing and removing what is unused.
Yes. It records the expected checksums, so builds elsewhere detect tampered or changed modules.
Not for dependency management. Modules replaced the GOPATH workflow, and GOPATH is now just the location of the module cache and installed binaries.
Delete the imports from your code and run go mod tidy. The requirement is removed from go.mod automatically.
Go modules give reproducible builds with just go.mod and go.sum. Initialise with go mod init, add and pin with go get, clean with go mod tidy, vendor only when you need it, and scan with govulncheck. Commit both module files and test after every upgrade.