Handle replace exclude and retract
Handle replace exclude and retract
replace substitutes module contents; it does not add a requirement by itself. exclude in the main go.mod disallows a particular module version. retract is published by a module author to mark problematic versions; a retraction is advisory and does not automatically remove an already selected version. Replacements and exclusions in dependencies do not control the main module.
Use a fresh directory for each example, create the listed files (labels are not file contents), and run the commands in a POSIX shell. Use Go 1.23+, local toolchain, and Python 3 only for the synthetic proxy examples. For isolation, set GOWORK=off, GOTOOLCHAIN=local, GOPROXY=off and GOSUMDB=off for these local labs only; use a fresh GOMODCACHE per lab. Proxy examples override GOPROXY to a local file URL. Do not copy disabled checksum settings to real projects.
--- go.mod ---module example.test/app
go 1.23.0
require example.test/lib v1.0.0replace example.test/lib => ./lib
--- main.go ---package main
import ( "example.test/lib" "fmt")
func main() { fmt.Println(lib.Version()) }
--- lib/go.mod ---module example.test/lib
go 1.23.0
--- lib/lib.go ---package lib
func Version() string { return "local" }
--- Commands (shell, from project root) ---go list -m -f '{{.Path}} {{.Version}} => {{.Replace.Path}}' example.test/libgo run .The import stays example.test/lib, but content comes from ./lib. The directory needs its own go.mod. Local replacements are useful for development, but consumers do not inherit them; avoid publishing a module whose usability depends on a local path.
Expected output:
example.test/lib v1.0.0 => ./liblocalInspect retracted and excluded versions
--- proxy.py ---from pathlib import Pathimport json, zipfileroot=Path("proxy")for version in ("v1.0.0", "v1.1.0", "v1.2.0", "v1.3.0", "v1.2.1-0.20240102030405-abcdef123456"): path="example.test/lib" d=root/path/"@v"; d.mkdir(parents=True,exist_ok=True) mod="module "+path+"\n\ngo 1.23.0\n" if version=="v1.3.0": mod+="\nretract v1.2.0 // demonstration\n" (d/(version+".mod")).write_text(mod) (d/(version+".info")).write_text(json.dumps({"Version":version,"Time":"2024-01-02T03:04:05Z"})) with zipfile.ZipFile(d/(version+".zip"),"w") as z: prefix=path+"@"+version+"/" z.writestr(prefix+"go.mod",mod) z.writestr(prefix+"lib.go",'package lib\nfunc Version() string { return "'+version+'" }\n') (d/"list").write_text("v1.0.0\nv1.1.0\nv1.2.0\nv1.3.0\n")
--- go.mod ---module example.test/app
go 1.23.0
exclude example.test/lib v1.1.0
--- main.go ---package main
import ( "example.test/lib" "fmt")
func main() { fmt.Println(lib.Version()) }
--- Commands (shell, from project root) ---python3 proxy.pyexport GOPROXY="file://$PWD/proxy" GOSUMDB=off GONOSUMDB=none GOPRIVATE= GONOPROXY=nonego list -m -versions example.test/libgo list -m -versions -retracted example.test/libThe latest synthetic release v1.3.0 retracts v1.2.0. The version listing omits both locally excluded v1.1.0 and retracted v1.2.0 by default. With -retracted, v1.2.0 appears again but v1.1.0 remains excluded. Inspect requirements before changing them.
Expected output:
example.test/lib v1.0.0 v1.3.0example.test/lib v1.0.0 v1.2.0 v1.3.0Try it locally
Exclude v1.1.0 and explicitly choose v1.0.0 in a fresh module. Print the version and the exclude directive.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Modules and workspaces 06: Handle replace exclude and retract
Check the lesson objective before moving on.
Does replace alone add a module to the build list?
Who normally publishes a retract directive?