Here I intend to summarize how the Go package cache system works, after spending half of my afternoon running tests and figuring out this weird thing.
First off, to create a new Go project outside the awful GOPATH
directory, you run:
mkdir my-project cd my-project go mod init my-project
This will create a go.mod
file.
To add a dependency from GitHub, you run:
go get github.com/username/reponame
This will create a go.sum
file, which contains the dependency tree. The package files themselves will be downloaded and buried within ~/go/pkg/mod
directory, so they are effectively cached at global, machine-wide scope.
To list the packages inside my-project
, run:
go list ./...
This won’t list globally downloaded, cached dependencies installed with go get
.
To list the packages installed (not the cached ones) globally, run:
go list ...
Downloaded dependencies will be listed if you are in my-project
folder, and if they have been downloaded already. If you run the command above from within a project directory, it will download and install any missing packages for current project, and then they will be listed.
So, if you’re outside my-project
directory, there’s no way to list globally cached packages.
There’s no way to remove a globally cached package from cache individually. All you can do is delete the whole cache, with:
go clean -cache -modcache -i -r
No comments:
Post a Comment