Why you should use go build -a (or gb)
Update: The problem was fixed in Go 1.5 (thanks Nicolas Grilly). I was accidentally testing with Go 1.4 when writing this article 😩.
The go build tool is pretty great compared other platforms, but there are a few problems with it when it comes to reproducible builds.
Consider the following scenario:
As you can see above, it’s possible for go build to “successfully” build a binary, even so one of the files it depends on was deleted. This is happening because go build compares the file modification time of the previously built pkg binary with the file times of all the files in the pkg directory, but not the modification time of the pkg directory itself.
So if you’re looking to improve your builds, especially for your CI setup, you should always use `go build -a`.
That being said, I’m not sure why this hasn’t been fixed in go build itself yet, if anybody knows the reason I’ll update this article.