Here’s an easy tip for reducing the size of binaries in Go.
It’s as simple as specifying a couple of flags for the linker, -ldflags="-s -w"
, and results in the following reduction:
$ go build -o hello_gophers hello_gophers.go
$ stat -f "%z %N" hello_gophers
7372276 hello_gophers
$ go build -o hello_gophers -ldflags="-s -w" hello_gophers.go
$ stat -f "%z %N" hello_gophers
5823988 hello_gophers
As described in the documentation, -s
omits the symbol table and debug information, and -w
omits DWARF debugging information.
That’s not a bad savings, for adding some flags. Even greater compression has been achieved by using UPX, at the cost of increased startup time.