TExcelTAD
Home › Go Modules in Depth › Semantic Import Versioning

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:

bash · try it yourself
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/chi
go: 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 still chi and quote.
  • go list -m -u doesn't offer v5 as an upgrade for github.com/go-chi/chi: v5 is a different module. It does flag v1 as deprecated, because chi's last v1 go.mod has 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, init functions). 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:

bash · try it yourself
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@latest
go: 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:

  1. Read the release notes and the new API.
  2. Rewrite imports, github.com/go-chi/chi → github.com/go-chi/chi/v5 (a sed over *.go, or gofmt -r for renames), fixing the code that breaks.
  3. go mod tidy adds the v5 requirement and removes v1 once nothing imports it.
  4. 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.x versions 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 tagged v2.0.0 or later before modules existed, without a go.mod, is still usable; such versions show up as v2.0.0+incompatible under the old path. It's a compatibility bridge for old code. A module with a go.mod can never get +incompatible versions.
  • gopkg.in/yaml.v3 style 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?

  1. import "rsc.io/quote@v3"
  2. import "rsc.io/quote/v3"
  3. import "rsc.io/quote" with require rsc.io/quote v3.1.0
  4. import quote3 "rsc.io/quote"

Can one program use rsc.io/quote v1 and rsc.io/quote/v3 at the same time?

  1. No, MVS picks one
  2. Yes: they are different modules, each with its own selected version
  3. Only with replace
  4. Only in a workspace

Why doesn't go get -u upgrade chi from v1.5.5 to v5?

  1. v5 is retracted
  2. github.com/go-chi/chi/v5 is a different module; you switch by changing imports
  3. go get -u never changes minor versions
  4. Because of go.sum