liveBook · Manning

Go mod tidy

As explained in Go in Practice, Second Edition

go mod tidy is a Go command that adds missing module requirements and removes unnecessary ones from module metadata.

go mod tidy cleans Go module metadata after dependencies change. It re-walks the dependency graph so the packages required by the project remain compatible and satisfied. section 6.1.3 section 1.2.4

Use the command as part of a dependency-maintenance workflow:

  • Initialize the project with go mod init, which creates go.mod and records the Go version and dependencies. section 1.2.4
  • Select a specific dependency version with go get [package]@0.1.1 when the latest release is not wanted. section 6.1.3
  • Update all dependencies with go get -u, then run go mod tidy to clean and reconcile the dependency tree. section 6.1.3
  • Update one dependency with go get -u [package], remembering that transitive dependencies may also change. section 6.1.3
  • Inspect resulting versions and their dependencies with go mod graph; use go get -t when test-only packages matter. section 6.1.3

Refresh module dependencies

$ go get -u $ go mod tidy $ go mod graph section 6.1.3

How Go in Practice, Second Edition covers it

How deep it goes
Explains the command’s dependency-graph walk and workflow role, but stops short of internal resolution algorithms.
Examples
Uses go get -u followed by go mod tidy, with go mod graph for inspecting resulting dependency versions.
What it assumes
Assumes a Go project initialized with go mod init and dependencies managed through go get.
What you can do after
Operate/configure module metadata and verify dependency changes after updating or removing requirements.
How it connects
Connects module cleanup with go mod init, go get, semantic versioning, transitive dependencies, and go mod graph.

Also explained in

Compare how 2 books explain go mod tidy →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Go in Practice, Second Edition. The section links point to the parts of the book it draws on.