Semantic Import Versioning
Minimal version selection only works if a newer version is always safe to use in place of an older one. Go makes that a rule of the ecosystem, the import compatibility rule:
If an old package and a new package have the same import path, the new package must be backwards compatible with the old package.
So a breaking change can't keep the import path. From v2 on, the major version is part of the module path: github.com/go-chi/chi (v0/v1) and github.com/go-chi/chi/v5 (v5) are, for the go command, two unrelated modules. That's semantic import versioning.
| Version | Module path | Import path of the root package |
|---|---|---|
| v0.x.y, v1.x.y | rsc.io/quote |
rsc.io/quote |
| v2.x.y | rsc.io/quote/v2 |
rsc.io/quote/v2 |
| v3.x.y | rsc.io/quote/v3 |
rsc.io/quote/v3 |
The package name doesn't change: code in rsc.io/quote/v3 still says package quote, so after import "rsc.io/quote/v3" you write quote.HelloV3().
Two majors in one build
Because they are different modules, one program can contain v1 and v3 of the same library, and MVS never has to choose between them. That is how large codebases migrate one package at a time. This module uses rsc.io/quote v1 and v3, and the chi router v1 and v5:
mkdir siv && cd siv
go mod init example.com/siv 2>/dev/null
cat > main.go <<'SRC'
package main
import (
"fmt"
"net/http"
"net/http/httptest"
"strings"
chiv1 "github.com/go-chi/chi"
"github.com/go-chi/chi/v5"
"rsc.io/quote"
quotev3 "rsc.io/quote/v3"
)
func main() {
fmt.Println("quote v1:", quote.Go())
fmt.Println("quote v3:", quotev3.GoV3())
old := chiv1.NewRouter()
old.Get("/old", func(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, "served by chi v1") })
r := chi.NewRouter()
r.Mount("/", old) // a v5 router can mount any http.Handler, including a v1 router
r.Get("/hi/{name}", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "chi v5 says hi to ", chi.URLParam(r, "name"))
})
for _, path := range []string{"/old", "/hi/ada"} {
rec := httptest.NewRecorder()
r.ServeHTTP(rec, httptest.NewRequest("GET", path, nil))
fmt.Println(path, rec.Code, strings.TrimSpace(rec.Body.String()))
}
}
SRC
go mod tidy
go run .
go list -m all
go list -m -u github.com/go-chi/chigo: finding module for package github.com/go-chi/chi
go: finding module for package rsc.io/quote/v3
go: finding module for package rsc.io/quote
go: finding module for package github.com/go-chi/chi/v5
go: downloading github.com/go-chi/chi/v5 v5.5.0
go: downloading github.com/go-chi/chi v1.5.5
go: downloading rsc.io/quote v1.5.2
go: downloading rsc.io/quote/v3 v3.1.0
go: found github.com/go-chi/chi in github.com/go-chi/chi v1.5.5
go: found github.com/go-chi/chi/v5 in github.com/go-chi/chi/v5 v5.5.0
go: found rsc.io/quote in rsc.io/quote v1.5.2
go: found rsc.io/quote/v3 in rsc.io/quote/v3 v3.1.0
go: downloading rsc.io/sampler v1.3.0
go: downloading golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c
quote v1: Don't communicate by sharing memory, share memory by communicating.
quote v3: Don't communicate by sharing memory, share memory by communicating.
/old 200 served by chi v1
/hi/ada 200 chi v5 says hi to ada
example.com/siv
github.com/go-chi/chi v1.5.5
github.com/go-chi/chi/v5 v5.5.0
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c
rsc.io/quote v1.5.2
rsc.io/quote/v3 v3.1.0
rsc.io/sampler v1.3.0
github.com/go-chi/chi v1.5.5 (deprecated)
Things to notice:
- Two import aliases (
chiv1,quotev3) keep the names apart; the package names are stillchiandquote. go list -m -udoesn't offer v5 as an upgrade forgithub.com/go-chi/chi: v5 is a different module. It does flag v1 as deprecated, because chi's last v1go.modhas a// Deprecated:comment pointing to v5. Deprecation notices are the main way users discover a new major version.- Each major version keeps its own global state (package-level variables, registries,
initfunctions). Two majors in one binary are two copies of the code.
You can't get v5 with the v1 path
Upgrading across a major version is not a go get -u. You change the imports, and the requirement follows:
mkdir m && cd m
go mod init example.com/m 2>/dev/null
go get github.com/go-chi/chi@v5.5.0
go get github.com/go-chi/chi/v5@v1.5.5
go get github.com/go-chi/chi/v5@latestgo: github.com/go-chi/chi@v5.5.0: reading file:///opt/goproxy/github.com/go-chi/chi/@v/v5.5.0.info: no such file or directory
go: downloading github.com/go-chi/chi v1.5.5
go: module github.com/go-chi/chi@v1.5.5 found, but does not contain package github.com/go-chi/chi/v5
go: downloading github.com/go-chi/chi/v5 v5.5.0
go: added github.com/go-chi/chi/v5 v5.5.0
The first error comes from our file proxy, which simply has no such file. proxy.golang.org answers with a similar "not found"/"invalid version" message: a module path without /v5 can only have v0 and v1 versions. The second shows the rule from the other side: v1.5.5 can only belong to the v0/v1 module github.com/go-chi/chi, so the go command treats chi/v5 as a package inside that module, downloads it, and finds no such package.
The migration recipe for a major upgrade:
- Read the release notes and the new API.
- Rewrite imports,
github.com/go-chi/chi→github.com/go-chi/chi/v5(asedover*.go, orgofmt -rfor renames), fixing the code that breaks. go mod tidyadds the v5 requirement and removes v1 once nothing imports it.- Run the tests. Large codebases can do steps 2–4 package by package, with both majors in the build meanwhile.
v0, v1 and +incompatible
- v0 promises nothing:
v0.xversions may break between minor releases. Use v0 until the API settles. - v1 is the stability promise. Breaking v1 means making a v2.
+incompatible: a repository that taggedv2.0.0or later before modules existed, without ago.mod, is still usable; such versions show up asv2.0.0+incompatibleunder the old path. It's a compatibility bridge for old code. A module with ago.modcan never get+incompatibleversions.gopkg.in/yaml.v3style paths (major version after a dot) are an older convention that the go command still understands.
The rules for publishing your own v2 are in the next lesson, Releasing a v2.
Test yourself
How do you import version 3 of rsc.io/quote?
import "rsc.io/quote@v3"import "rsc.io/quote/v3"import "rsc.io/quote"withrequire rsc.io/quote v3.1.0import quote3 "rsc.io/quote"
Can one program use rsc.io/quote v1 and rsc.io/quote/v3 at the same time?
- No, MVS picks one
- Yes: they are different modules, each with its own selected version
- Only with replace
- Only in a workspace
Why doesn't go get -u upgrade chi from v1.5.5 to v5?
- v5 is retracted
github.com/go-chi/chi/v5is a different module; you switch by changing imports- go get -u never changes minor versions
- Because of go.sum