Reduce Go binary size

I will show a simple way to reduce Go binary size

Dev env:
go version go1.11.5 darwin/amd64 Mac OSX 10.14 & Debian9 64bit
Don’t have UPX on your machine? Install it via brew install upx

Let get started 😀

// main.go
package main
 import "fmt"
 func main() {
     fmt.Println("hello world")
}

// Build 
go build main.go && ls -la main
// Return
-rwxr-xr-x  1 lol  staff  2003480 Feb 18 01:14 main

Lets reduce the size

go build -ldflags "-s -w" main.go && ls -la main
// Return
-rwxr-xr-x  1 lol  staff  1585688 Feb 18 01:17 main

// NOW magic happen
// Use UPX shell to compress it again
upx --brute main

//return ? suprized? ?
1585688 ->    462864   29.19%   macho/amd64   main
-rwxr-xr-x  1 lol  staff  462864 Feb 18 01:17 main

Almost 1/5th of their size

H

References:
1. https://gobyexample.com/hello-world
2. https://github.com/upx/upx/releases