27

What is the different between go mod download and go mod tidy?

When I run go mod tidy, if the import is not found it also downloads it.

blackgreen
  • 34,072
  • 23
  • 111
  • 129
dave
  • 867
  • 1
  • 5
  • 11
  • maybe not a perfect duplicate, but it provides useful info: https://stackoverflow.com/questions/66356034 – blackgreen Mar 16 '22 at 10:45
  • 1
    Basically the quote from [here](https://pkg.go.dev/cmd/go#hdr-Download_modules_to_local_cache): *The "go mod download" command is useful mainly for pre-filling the local cache or to compute the answers for a Go module proxy.* – blackgreen Mar 16 '22 at 10:49

1 Answers1

28

go mod download is downloading all of the modules in the dependency graph, which it can determine from reading only the go.mod files. It doesn't know which of those modules are actually needed to satisfy a build, so it doesn't pull in the checksums for those modules (because they may not be relevant).

On the other hand, go mod tidy has to walk the package graph in order to ensure that all imports are satisfied. So it knows, for a fact, without doing any extra work, that the modules it walks through are needed for a go build in some configuration.

Puji Setiawan
  • 321
  • 2
  • 4