Go modules
A module is a versioned tree of Go packages identified by a module path, usually a repository URL. It is the unit you tag, download, and import; a package is one folder inside it.
Detailed Explanation
Before modules, Go looked up imports on GOPATH. Two projects on one laptop shared one tree of dependencies, and "it works on my machine" meant "we happened to have the same checkout." Modules replaced that with a per-project graph. Each module has a path (example.com/shop), a version (v1.2.3), and a go.mod that lists what it needs.
Packages live inside modules. import "example.com/shop/internal/httpapi" means: find the module whose path is a prefix of that import (example.com/shop), then walk into internal/httpapi. The standard library is a module the toolchain already has. Everything else is downloaded into the module cache ($GOPATH/pkg/mod) and selected by Minimal Version Selection.
Minimal Version Selection (MVS) is the rule that picks versions. If your module requires A v1.2.0 and another dependency requires A v1.3.0, the build uses v1.3.0 — the maximum of the minimums. It never automatically takes "latest." That is the opposite of many JavaScript resolvers, and it is why adding a dependency does not silently jump your other libraries to their newest majors. You raise a version with go get.
Semantic import versioning. For module paths at v2 and above, the major version is part of the import path: example.com/shop/v2. example.com/shop and example.com/shop/v2 are different modules and can be imported together. v0 and v1 omit the suffix. Tag your repository v2.0.0 only if the go.mod module line already ends in /v2.
The main module is the one containing the current working directory. Commands such as go test and go build apply replace directives and the go version line from that module. When someone imports your library, they are not building your module as main, so your replace lines do not apply to them.
Retract and deprecation. retract v1.2.3 in go.mod tells go get to skip a bad release. It does not yank the bits from the cache of people who already have it. Use it after you publish a version that does not compile.
What it costs. Modules make builds reproducible and remove GOPATH as a shared junk drawer. The cost is a version graph you must keep tidy, and a major-version path rule that surprises people coming from languages where v2 is only a tag. For a single binary in one repo you still want a module; workspaces exist when that repo holds several modules you develop together.
Code Examples
# Start a module in the current directory. The argument is the import-path prefix.
go mod init example.com/shop
# Add a dependency at a chosen version, then drop unused requires.
go get golang.org/x/sync@v0.8.0
go mod tidy
# See why a module is in the graph.
go mod why golang.org/x/syncpackage main
import (
"fmt"
"golang.org/x/sync/errgroup"
)
func main() {
// This import resolves through the module graph in go.mod,
// not through GOPATH.
var g errgroup.Group
fmt.Printf("%T\n", g)
}# v2+ module path includes the major version.
# module example.com/shop/v2
#
# Importers write:
# import "example.com/shop/v2/httpapi"Common Gotchas
A v2+ module whose `module` line lacks `/v2` will not match its tags
importers looking for `example.com/shop/v2` will not find a module named `example.com/shop`. Fix the module path and retag.
MVS will not upgrade you to "latest" on its own
a new major of a dependency stays unused until some `go.mod` in the graph requires it. That is intentional; run `go get` when you want the upgrade.
Pseudo-versions (`v0.0.0-20240101120000-abcdef`) are commits, not releases
they work, but they are harder to audit than a tagged `v1.2.3`. Prefer tags in libraries other people import.
`GOPATH` mode is gone for current toolchains
if `go.mod` is missing, recent Go versions still try to infer a module, or they fail. Add `go.mod` rather than setting `GO111MODULE=off`.