TExcelTAD
Home › Go Modules in Depth › Upgrades, exclude & retract

Upgrades, exclude & retract

Sooner or later a version goes bad: a release with a bug, a tag pushed by mistake, a dependency that breaks your build. Go gives the author of a module one tool and the user another:

Who Tool Where Effect
the module's author retract in the module's own go.mod, in a newer release @latest skips the version; users of it get a warning
the consumer exclude in the main module's go.mod that version can't be selected in this build
the consumer replace in the main module's go.mod use other code for a version (replace for Local Development)

Versions are never deleted: proxies keep everything they have served (deleting would break everyone's go.sum). Retraction is the way to say "don't use this".

A module proxy you can write by hand

To publish versions in a session we need a proxy we control. A GOPROXY is just a file tree, served over HTTPS or read from disk with file://:

text
<proxy>/example.com/greet/@v/list          one version per line
<proxy>/example.com/greet/@v/v1.0.0.info   {"Version":"v1.0.0","Time":"…"}
<proxy>/example.com/greet/@v/v1.0.0.mod    the go.mod of that version
<proxy>/example.com/greet/@v/v1.0.0.zip    the files, each under example.com/greet@v1.0.0/

(Module paths with capital letters are escaped as !x; our paths are lowercase.) The session below writes gopublish, a short standard-library program that turns a directory into those four files, and the rest of the course reuses it. Nothing about it is special: real proxies like Athens or Artifactory serve the same layout (Private Modules & Proxies).

retract: the author withdraws a version

The author of example.com/greet publishes v1.0.0, then a broken v1.1.0, then v1.2.0, whose go.mod retracts v1.1.0. Retractions are read from the latest version's go.mod, which is why the retraction ships in a new release.

bash · try it yourself
# gopublish DIR MODULE VERSION PROXYDIR: write one module version in GOPROXY layout
mkdir -p ~/tools/gopublish && cat > ~/tools/gopublish/main.go <<'SRC'
package main

import (
	"archive/zip"
	"os"
	"path/filepath"
)

func main() {
	dir, mod, ver := os.Args[1], os.Args[2], os.Args[3]
	at := filepath.Join(os.Args[4], mod, "@v")
	os.MkdirAll(at, 0o755)
	gomod, _ := os.ReadFile(filepath.Join(dir, "go.mod"))
	os.WriteFile(filepath.Join(at, ver+".mod"), gomod, 0o644)
	info := `{"Version":"` + ver + `","Time":"2026-01-15T10:00:00Z"}`
	os.WriteFile(filepath.Join(at, ver+".info"), []byte(info), 0o644)
	f, _ := os.Create(filepath.Join(at, ver+".zip"))
	z := zip.NewWriter(f)
	filepath.WalkDir(dir, func(p string, d os.DirEntry, err error) error {
		if !d.IsDir() {
			rel, _ := filepath.Rel(dir, p)
			w, _ := z.Create(mod + "@" + ver + "/" + filepath.ToSlash(rel))
			data, _ := os.ReadFile(p)
			w.Write(data)
		}
		return nil
	})
	z.Close()
	f.Close()
	list, _ := os.OpenFile(filepath.Join(at, "list"), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o644)
	list.WriteString(ver + "\n")
	list.Close()
}
SRC
(cd ~/tools/gopublish && go mod init gopublish 2>/dev/null && go build -o ~/go/bin/gopublish .)
export GOPROXY=file://$HOME/proxy,file:///opt/goproxy
mkdir -p src/greet
printf 'module example.com/greet\n\ngo 1.27\n' > src/greet/go.mod
say() { printf 'package greet\n\nfunc Hello() string { return "%s" }\n' "$1" > src/greet/greet.go; }
say "hello from v1.0.0"; gopublish src/greet example.com/greet v1.0.0 proxy
say "oops (v1.1.0)";     gopublish src/greet example.com/greet v1.1.0 proxy
printf '\n// Hello returned the wrong text.\nretract v1.1.0\n' >> src/greet/go.mod
say "hello from v1.2.0"; gopublish src/greet example.com/greet v1.2.0 proxy
find proxy -type f | sort
cat proxy/example.com/greet/@v/v1.2.0.mod
mkdir app && cd app
go mod init example.com/app 2>/dev/null
printf 'package main\n\nimport (\n\t"fmt"\n\n\t"example.com/greet"\n)\n\nfunc main() { fmt.Println(greet.Hello()) }\n' > main.go
go get example.com/greet@v1.1.0
go run .
go list -m -u all
go list -m -versions example.com/greet
go list -m -retracted -versions example.com/greet
go get example.com/greet@latest
go run .
proxy/example.com/greet/@v/list
proxy/example.com/greet/@v/v1.0.0.info
proxy/example.com/greet/@v/v1.0.0.mod
proxy/example.com/greet/@v/v1.0.0.zip
proxy/example.com/greet/@v/v1.1.0.info
proxy/example.com/greet/@v/v1.1.0.mod
proxy/example.com/greet/@v/v1.1.0.zip
proxy/example.com/greet/@v/v1.2.0.info
proxy/example.com/greet/@v/v1.2.0.mod
proxy/example.com/greet/@v/v1.2.0.zip
module example.com/greet

go 1.27

// Hello returned the wrong text.
retract v1.1.0
go: downloading example.com/greet v1.1.0
go: warning: example.com/greet@v1.1.0: retracted by module author: Hello returned the wrong text.
go: to switch to the latest unretracted version, run:
	go get example.com/greet@latest
go: added example.com/greet v1.1.0
oops (v1.1.0)
example.com/app
example.com/greet v1.1.0 (retracted) [v1.2.0]
example.com/greet v1.0.0 v1.2.0
example.com/greet v1.0.0 v1.1.0 v1.2.0
go: downloading example.com/greet v1.2.0
go: upgraded example.com/greet v1.1.0 => v1.2.0
hello from v1.2.0

What happened:

  • go get …@v1.1.0 still works (you asked for exactly that version), but prints the author's rationale and the command to move on.
  • go list -m -u marks the version (retracted) and offers v1.2.0. CI can grep for that.
  • -versions hides retracted versions; -retracted shows them.
  • @latest never picks a retracted version.

An author can even retract the version that carries the retraction, e.g. a v1.0.0 tagged by accident on a module that should still be at v0: publish v1.0.1 with retract [v1.0.0, v1.0.1], and @latest falls back to the highest unretracted version.

exclude: the consumer refuses a version

exclude is for when you know a version is bad for your build and its author hasn't retracted it (or never will). Here the app has upgraded everything and doesn't want sampler v1.99.99:

bash · try it yourself
mkdir app && cd app
go mod init example.com/app 2>/dev/null
printf 'package main\n\nimport (\n\t"fmt"\n\n\t"rsc.io/quote"\n)\n\nfunc main() { fmt.Println(quote.Hello()) }\n' > main.go
go get rsc.io/quote@v1.5.2 2>/dev/null
go mod edit -exclude=rsc.io/sampler@v1.99.99
go get -u ./...
go run .
cat go.mod
go get rsc.io/sampler@v1.99.99
go: downloading rsc.io/sampler v1.3.1
go: downloading golang.org/x/text v0.43.0
go: upgraded golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c => v0.43.0
go: upgraded rsc.io/sampler v1.3.0 => v1.3.1
Ahoy, world!
module example.com/app

go 1.27.3

require rsc.io/quote v1.5.2

require (
	golang.org/x/text v0.43.0 // indirect
	rsc.io/sampler v1.3.1 // indirect
)

exclude rsc.io/sampler v1.99.99
go: rsc.io/sampler@v1.99.99: excluded by go.mod

With the exclusion, go get -u skips v1.99.99 and takes the next highest release, v1.3.1. Asking for the excluded version explicitly fails. Two limits to remember:

  • An exclusion only works in the main module. If your library excludes a version, your users don't inherit it; their build can still select it.
  • If a dependency requires the excluded version, the go command moves to the next higher version that isn't excluded, not to a lower one.

Test yourself

The author of a module published a broken v1.1.0. What should they do?

  1. Delete the tag and the version from the proxy
  2. Publish a newer version whose go.mod has retract v1.1.0
  3. Ask users to add exclude lines
  4. Re-tag v1.1.0 with the fix

Where does the go command read retractions from?

  1. The go.mod of the retracted version
  2. The go.mod of the module's latest version
  3. The checksum database
  4. The main module's go.mod

Your library's go.mod has exclude rsc.io/sampler v1.99.99. Does that protect the applications that use your library?

  1. Yes, exclusions are inherited
  2. No, exclude only applies in the main module
  3. Only if they vendor
  4. Only with GOFLAGS=-mod=mod