go build and go run are very slow on a tiny program I have (cgo invocations in particular). I\'d like go to cache the binary so that it only rebuilds when the source is new
I wrote a tool that happens to solve this as a side effect. go build
alone will not check if the executable it's producing is already up to date. go install
does, and if you tweak it to install to a location of your choice, then you'll get the desired result, similar to go build
.
You can see the behaviour you describe by doing something like this:
$ go get -d github.com/anacrolix/missinggo/cmd/nop
$ time go run "$GOPATH"/src/github.com/anacrolix/missinggo/cmd/nop/*.go
real 0m0.176s
user 0m0.142s
sys 0m0.048s
That's on a warm run. go run
will link on every invocation, just as go build
would. Note that github.com/anacrolix/missinggo/cmd/nop
is an program that does absolutely nothing.
Here's invoking the same package, using my tool, godo:
$ time godo github.com/anacrolix/missinggo/cmd/nop
real 0m0.073s
user 0m0.029s
sys 0m0.033s
For larger programs, the difference should be more pronounced.
So in summary, your standard tooling option is to use go install
, or an alternative like godo.
go build
and go install
will soon (Go 1.10, Q1 2018) be much faster: see this thread and this draft document.
The go command now maintains a cache of built packages and other small metadata (CL 68116 and CL 75473). The cache defaults to the operating system-defined user cache directory but can be moved by setting
$GOCACHE
.
Run "go env GOCACHE
" to see the current effective setting. Right now the go command never deletes anything from the cache. If the cache gets too big, run "go clean -cache
" instead of deleting the directory. That command will preserve the cache'slog.txt
file. In a few weeks I'll ask people to post theirlog.txt
files to a Github issue so that we can evaluate cache size management approaches.The main effect of the build cache is that commands like "
go test
" and "go build
" run fast and do incremental builds always, reusing past build steps as aggressively as possible.
You do not have to use "go test -i
" or "go build -i
" or "go install
" just to get fast incremental builds. We will not have to teach new users those workarounds anymore. Everything will just be fast.
Note that go install
won't installs dependencies of the named packages: see "What does go build build?".