go build, go install, and go test accept the -i flag. From go help build:
The -i flag installs the packages that are dependencies of the target.
For example, if you run go build -i example.com/a, and example.com/x imports example.com/y, go build installs the file $GOPATH/pkg/$GOOS_$GOARCH/example.com/y.a.
This was useful for speeding up builds before Go 1.10, since previously installed packages didn't need to be recompiled. go build re-used packages installed in $GOPATH/pkg.
Go 1.10 introduced the build cache, so the $GOPATH/pkg directory is largely obsolete. Compiled packages are now stored in $GOCACHE. The -i flag merely copies files out of the build cache, so there's no longer any performance advantage.
The -i can cause errors when a target directory is not writable, as in #37962. In that issue, VSCode installed tools with -i, which caused errors when $GOROOT/pkg was not writable (common when Go is installed system-wide with an installer). Something caused runtime/cgo to be recompiled (perhaps a new clang version or flag), but it couldn't be written to $GOROOT/pkg/darwin_amd64/runtime/cgo.a.
Because of these problems, we should consider deprecating and eventually removing the -i flag.
go build,go install, andgo testaccept the-iflag. Fromgo help build:For example, if you run
go build -i example.com/a, andexample.com/ximportsexample.com/y,go buildinstalls the file$GOPATH/pkg/$GOOS_$GOARCH/example.com/y.a.This was useful for speeding up builds before Go 1.10, since previously installed packages didn't need to be recompiled.
go buildre-used packages installed in$GOPATH/pkg.Go 1.10 introduced the build cache, so the
$GOPATH/pkgdirectory is largely obsolete. Compiled packages are now stored in$GOCACHE. The-iflag merely copies files out of the build cache, so there's no longer any performance advantage.The
-ican cause errors when a target directory is not writable, as in #37962. In that issue, VSCode installed tools with-i, which caused errors when$GOROOT/pkgwas not writable (common when Go is installed system-wide with an installer). Something causedruntime/cgoto be recompiled (perhaps a newclangversion or flag), but it couldn't be written to$GOROOT/pkg/darwin_amd64/runtime/cgo.a.Because of these problems, we should consider deprecating and eventually removing the
-iflag.