Where is the module cache in golang?

后端 未结 5 1058
我在风中等你
我在风中等你 2021-01-01 11:38

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/s

相关标签:
5条回答
  • 2021-01-01 11:58

    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.

    0 讨论(0)
  • 2021-01-01 11:59

    For Go 1.11, they are stored in

    $GOPATH/pkg/mod
    
    0 讨论(0)
  • 2021-01-01 12:00

    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.

    0 讨论(0)
  • 2021-01-01 12:04

    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
    0 讨论(0)
  • 2021-01-01 12:07

    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

    0 讨论(0)
提交回复
热议问题