Go Build's Unnecessary Rebuilds
Go's build process can be notably slow, especially for programs involving Cgo invocations. To address this, it's tempting to leverage a Makefile with % rules for caching. However, according to the language designers, Go's build support has made Makefiles obsolete.
Alternative Solutions
The go community prefers an alternative solution: a build cache maintained by the go command itself. This cache stores built packages and metadata, optimizing the build process by reusing previous build steps whenever possible.
Implementation and Benefits
As of Go 1.10 (Q1 2018), go build and go install have implemented this build cache. The cache location can be set using the $GOCACHE environment variable. By default, it resides in the operating system's user cache directory. The go clean -cache command can be used to clear the cache without deleting the log file.
The build cache not only accelerates "go test" and "go build" commands but also enables incremental builds by default. Users no longer need to resort to workarounds like "go test -i" or "go build -i" to achieve fast incremental builds.
The above is the detailed content of Why Is Go's Build Cache a Better Alternative to Makefiles for Faster Builds?. For more information, please follow other related articles on the PHP Chinese website!