Reputation: 1337
I always build with -i flag to install packages and .a files are installed in GOPATH/pkg directory.
GOCACHE directory shown by go env GOCACHE
seems to store cache files as well.
What's difference between them?
And what I wanna know is both of them should be saved if I want to make build time faster?
Upvotes: 7
Views: 8494
Reputation: 417747
TLDR; The cache folder is internal to the go
tool and its working should be opaque to the user, and its purpose is to speed up builds and tests. For example if you use a version control system (such as git
), switching between branches or versions, the GOPATH/pkg
may only contain package files of one version. The go cache folder may contain (partially) compiled packages of multiple branches and versions, speeding up future builds when you switch between branches and versions.
The cache folder was introduced in Go 1.10:
The
go build
command now maintains a cache of recently built packages, separate from the installed packages in$GOROOT/pkg
or$GOPATH/pkg
. The effect of the cache should be to speed builds that do not explicitly install packages or when switching between different copies of source code (for example, when changing back and forth between different branches in a version control system). The old advice to add the-i
flag for speed, as ingo build -i
orgo test -i
, is no longer necessary: builds run just as fast without-i
. For more details, seego help cache
.
So you don't need to use -i
anymore for fast builds.
Some quotes from the output of go help cache
:
The go command caches build outputs for reuse in future builds. The default location for cache data is a subdirectory named go-build in the standard user cache directory for the current operating system. Setting the GOCACHE environment variable overrides this default, and running 'go env GOCACHE' prints the current cache directory.
The go command periodically deletes cached data that has not been used recently. Running 'go clean -cache' deletes all cached data.
The build cache correctly accounts for changes to Go source files, compilers, compiler options, and so on: cleaning the cache explicitly should not be necessary in typical use. However, the build cache does not detect changes to C libraries imported with cgo. If you have made changes to the C libraries on your system, you will need to clean the cache explicitly or else use the -a build flag (see 'go help build') to force rebuilding of packages that depend on the updated C libraries.
The go command also caches successful package test results. See 'go help test' for details. Running 'go clean -testcache' removes all cached test results (but not cached build results).
The cache folder is also used to store test results, so in some circumstances, the cached results may be presented without running the tests again.
Upvotes: 8
Reputation: 3547
Your questions is self-answering:
$ ls $(go env GOCACHE)
$ cat $(go env GOCACHE)/README
and
$ ls $(go env GOPATH)/pkg
As you can see - there is nothing similar between them:
More elaborate answer could be done by examining sources of go build
Upvotes: 2