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.