Golang中的模块缓存在哪里?

Golang中的模块缓存在哪里?

问题描述:

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
...

当我启用gomodules并构建go程序时,便会下载所需的软件包。 p>

但是我无法在 $ GOPATH / src / code>或 $ GOPATH / src / mod code>中找到它们。 p>

他们存储了吗? p>

  export GO111MODULE = on 
go mod init 
go构建main.go 
go:查找github.com/sirupsen/logrus v1.0.6 
go:下载github  .com / sirupsen / logrus v1.0.6 
 ... 
  code>  pre> 
  div>

For Go 1.11, they are stored in

$GOPATH/pkg/mod

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

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.