Nikolai Koudelia
Nikolai Koudelia

Reputation: 2639

"go build" became very slow after installing a new version of Go

After upgrading from Go 1.2.1 to 1.3 (Windows 7 64 bit) "go build" execution time has increased from around 4 to over 45 seconds. There were no other changes except the go version update. Switching off the virus scanner seems to have no effect. Any clues?

Upvotes: 38

Views: 27510

Answers (6)

Inanc Gumus
Inanc Gumus

Reputation: 27889

After Go 1.10, you'd just need to type go build. You'd not need to type: go build -i.

From the draft Go 1.10 document, here.


Build & Install

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.)

...

The go build command now maintains a cache of recently built packages, separate from the installed packages in $GOROOT/pkg or $GOPATH/pkg. The effect of the cache should be to speed builds that do not explicitly install packages or when switching between different copies of source code (for example, when changing back and forth between different branches in a version control system). The old advice to add the -i flag for speed, as in go build -i or go test -i, is no longer necessary: builds run just as fast without -i. For more details, see go help cache.

Upvotes: 6

user4466350
user4466350

Reputation:

Using go1.6,

Simply run go build -i.

It will compile all the dependencies and store them at $GOPATH/pkg/*/* as .a files.

Later when you run go run main.go, everything is much faster.

What s really great is that if you use vendored dependencies (IE: a vendor folder in your project), deps are built appropriately within $GOPATH/pkg/**/yourproject/vendor/**

So you don t have to go get install/get/whatever and have a mix of vendor / global dependencies.

I suspect you got to re-build .a files after deps update (glide update or smthg like this), but i did not test that yet.

Upvotes: 19

bullgare
bullgare

Reputation: 1763

You can build sqlite3 like this:

cd ./vendor/github.com/mattn/go-sqlite3/
go install

After that your project will b built much faster.

Upvotes: 1

Kokizzu
Kokizzu

Reputation: 26818

I have the exact same problem, running this command solves it:

go get -u -v github.com/mattn/go-sqlite3

Another tip: http://kokizzu.blogspot.co.id/2016/06/solution-for-golang-slow-compile.html

Upvotes: 27

Georgi-it
Georgi-it

Reputation: 3686

I just experienced the same problem - updating from 1.4 to 1.5. It seems that the olds versions are somehow incompatible or are being rebuild every time as go build -x shows. Executing go get -v invalidates all packages or refetches them, I am not quite sure and go build -x shows quite less output.

Upvotes: 3

JimB
JimB

Reputation: 109327

You probably have dependencies that are being recompiled each time. Try go install -a mypackage to rebuild all dependencies.

Removing $GOPATH/pkg also helps to ensure you don't have old object files around.

Building with the -x flag will show you if the toolchain is finding incompatible versions.

Upvotes: 59

Related Questions