Reduce Golang Binary Size by 20%

Nick Galbreath

Here’s how to reduce your golang binary size by 20%, for free.

I first read about this on Shrink your go binaries with this one weird trick by Filippo Valsorda. That’s from 2016, but I retested on golang 1.8.1.

Just add -ldflags="-s -w" to your build and watch the output size drop by 20%.

go build -ldflags="-s -w"

This strips away a bunch of symbols that are only used by debuggers. If you don’t use them, then everything else is the same, including panic stack traces.

This also makes gzip compression work better. By packaging the reduced golang binary in a .tar.gz results in a distribution 4x smaller than the original.

If you use Godownloader, these flags and packaging are all done by default and the final results are uploaded to GitHub releases. Couldn’t be easier.

The article also mentions using UPX to create self-decompressing binaries. It is interesting but it’s not much better than the ldflags + .tar.gz combination to justify the complexity.