The problem is that the standard library packages are built without flags.
CGO_ENABLED changes build flags and therefore it can't use the pre-built packages so most of the standard library needs to be rebuilt.
As the other answer mentioned,
go build -i will install the packages built with the new flags, but that won't really solve much because if you install packages built with
CGO_ENABLED=0, it will speed up all future builds with
CGO_ENABLED=0, but it will slow down all the builds without it.
Unfortunately the way pre-built packages are installed by default today is pretty inefficient because everything goes into the same directory under the same names regardless of how it's built. If you want to be able to have fast builds of go programs with different flags, besides doing
go build -i you also need to use the the
-installsuffix and/or the
-pkgdir flags. In the system I work in, we have a handful of different compilation modes, each mode has different flags (because of a lot of old C code we interface with) and each mode also has its own