Use tidy download graph why and verify
Use tidy download graph why and verify
Use go mod tidy to reconcile requirements and checksums with imports, including test dependencies. go mod download populates the module cache; go mod graph shows requirement edges; go mod why explains a shortest import path from the main module; go mod verify checks downloaded cache content. These commands have different purposes.
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.
--- 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
require example.test/lib v1.0.0
--- main.go ---package main
import "fmt"
func main() { fmt.Println("hello module") }
--- Commands (shell, from project root) ---python3 proxy.pyexport GOPROXY="file://$PWD/proxy" GOSUMDB=off GONOSUMDB=none GOPRIVATE= GONOPROXY=nonego mod tidyif grep -q "require example.test/lib" go.mod; then echo retained; else echo removed; figo run .The requirement is unused because the main package only imports fmt. tidy removes it. In real projects, test imports and platform-specific source files can retain requirements even if the current executable does not use them.
Expected output:
removedhello moduleInspect the dependency path
--- 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
require 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 mod download example.test/libgo mod tidygo mod graph | grep "^example.test/app example.test/lib@"go mod why -m example.test/libgo mod verifygraph describes module requirements, whereas why explains package import reachability. A graph may contain modules not needed by a specific package. verify checks the cache, not whether the dependency API is correct.
Expected output:
example.test/app example.test/lib@v1.1.0# example.test/libexample.test/appexample.test/liball modules verifiedTry it locally
Use the synthetic lib v1.0.0 in main. Download, tidy, print the graph edge and verify.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Modules and workspaces 05: Use tidy download graph why and verify
Check the lesson objective before moving on.
Which command removes unused requirements?
Which command explains why a module is needed through imports?