Before go 1.10
For go < 1.10, the go build
doesn't store the package compiled object files. The go install
does. One solution is to call go install
in the right package. It will go through all package dependencies, compile them if the timestamp of the file in the dependency package changed, store the object file and then link them all.
Example:
GOBIN="my project build directory" go install ./cmd/mycmd
Another solution is to use -i
flag, which is supported by build
and test
commands.
After go 1.10
go 1.10 adds new flag to go build
which does the "smart" caching.
The go build command now detects out-of-date packages purely based on the content of source files, specified build flags, and metadata stored in the compiled packages. Modification times are no longer consulted or relevant. The old advice to add -a to force a rebuild in cases where the modification times were misleading for one reason or another (for example, changes in build flags) is no longer necessary: builds now always detect when packages must be rebuilt. (If you observe otherwise, please file a bug.)
Read release notes for more information.