46

When I enable gomodules and build my go program then the required packages are downloaded.

But I cannot find them in $GOPATH/src/ or in $GOPATH/src/mod.

Where are they stored?

export GO111MODULE=on
go mod init
go build main.go 
go: finding github.com/sirupsen/logrus v1.0.6
go: downloading github.com/sirupsen/logrus v1.0.6
...

6 Answers 6

60

For Go 1.11, they are stored in

$GOPATH/pkg/mod
4
  • 9
    Note this is documented in go help modules Sep 1, 2018 at 14:37
  • 15
    On Linux, if $GOPATH is empty. Modules are stored in /home/<user>/go/pkg/mod!
    – ifnotak
    Jun 25, 2019 at 13:23
  • 1
    @ifnotak The same as macOS.
    – Allen
    Jul 12, 2020 at 15:40
  • GOPATH doesn't make sense when go modules are enabled. See stackoverflow.com/a/67233153/224021 for a current answer.
    – deubeulyou
    Apr 24, 2021 at 22:51
9

To determine cache locations, you can use go env:

go env|grep CACHE

To clean the caches, use go clean -modcache or go clean -cache depending on the type of cache to be cleared.

1
  • Thanks. go help clean also lists some other options.
    – PJ Brunet
    May 29 at 5:33
8

Run this in terminal

go env GOMODCACHE

go env - Print all Go environment information

go env NAME - Print specific env var.

2
  • 1
    You should include more of an explanation as part of your answer. The answer you provided is almost identical from an answer that was posted by Chris on Dec 30, 2020. Your answer should improve upon the existing answers.
    – Hermes
    Apr 23, 2021 at 17:48
  • I think this answer is the more accurate one for recent versions of go, and it does improve on Chris's answer.
    – deubeulyou
    Apr 24, 2021 at 22:49
7

I'm on Macos 10.13.6, using go1.11 darwin/amd64 and echo $GOPATH is empty.

I found my modules in $HOME/go/pkg/mod

2
  • do you have $GOPATH set to $HOME/go? Jun 19, 2019 at 11:27
  • 6
    $GOPATH defaults to $HOME/go on Go 1.11 and forward. Jul 3, 2019 at 16:15
7

The module cache is stored in $GOPATH/pkg/mod, or $HOME/go/pkg/mod if $GOPATH is not set.

Note: in general, the module cache is read-only, and is intended to be an immutable cache. As such, you should never try to edit things there, nor should you run go commands from inside the cache.

The module cache contains the zip files, unpacked module source code, as well as a VCS cache (when not using a proxy). The cache often contains multiple versions of a single dependency.

If you want to inspect the code of a dependency in the module cache, one shortcut is you can cd directly to the location of an unpacked dependency via:

cd $(go list -f '{{.Dir}}' -m github.com/foo/bar)

That asks go list to report on the directory location of the module github.com/foo/bar within the module cache, defaulting to whatever version you currently are using in your current module.

Given the cache is intended to be immutable, a related question is how do you edit a dependency (e.g., if you want to add a debug log, or perhaps in preparation for sending an upstream fix for a dependency). A common solution at this point is to use gohack, which creates a mutable copy of a dependency (by default in $HOME/gohack, but the location is controlled by $GOHACK variable). gohackalso sets your current go.mod file to have a replace directive to point to that mutable copy.

4

In case you want clean up cache there is a command go clean --modcache

In case you just need to update some dependency you probably want to clean up information about this dependency from

  • go.mod
  • go.sum
  • vendor/modules.txt

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.