How to Slash Go Binary Size by Up to 80%: Compiler Flags & UPX

Discover practical techniques to dramatically reduce the size of Go executables—starting with stripping debug symbols using ldflags, then applying UPX compression with optimal settings, and combining both methods to shrink a 6.5 MB binary down to around 1.4 MB, saving up to 80% space.

Radish, Keep Going!
Radish, Keep Going!
Radish, Keep Going!
How to Slash Go Binary Size by Up to 80%: Compiler Flags & UPX

Go is known for its fast compilation, but the size of the compiled binary can be surprisingly large. Using a simple HTTP server example, we explore how to reduce the binary size.

import (
    "fmt"
    "net/http"
)

func main() {
    // create a http server and handler that returns "Hello, World"
    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintf(w, "Hello, World
")
    })
    // listen on port 8080
    err := http.ListenAndServe(":8080", nil)
    if err != nil {
        return
    }
}

The initial compiled binary is 6.5 MB:

➜  binary-size git:(main) ✗ go build -o server main.go
➜  binary-size git:(main) ✗ ls -lh server
-rwxr-xr-x 1 hxzhouh staff 6.5M Jul 2 14:20 server

Eliminate Debug Information

By default, Go includes symbol tables and DWARF debug data. For release builds, these can be stripped using -ldflags="-s -w", reducing the binary to 4.5 MB (about 30% smaller).

➜  binary-size git:(main) ✗ go build -ldflags="-s -w" -o server main.go
➜  binary-size git:(main) ✗ ls -lh server
-rwxr-xr-x 1 hxzhouh staff 4.5M Jul 2 14:30 server
-s

: omit symbol table and debug info. -w: omit DWARF v3 debug info (cannot debug with gdb).

Use UPX

UPX is an advanced executable file compressor. It typically reduces the size of programs and DLLs by around 50%‑70%, lowering disk space, network load, and distribution costs.

Install UPX on macOS via Homebrew:

brew install upx

Compress with UPX Only

UPX offers compression levels 1‑9, where 1 is the fastest (lowest compression) and 9 is the slowest (highest compression). Using level 9 on the unmodified binary yields a 60% reduction:

➜  binary-size git:(main) ✗ go build -o server main.go && upx -9 server && ls -lh server
-rwxr-xr-x 1 hxzhouh staff 3.9M Jul 2 14:38 server

UPX + Compiler Flags

Combining the stripped build with UPX (using the --brute option) shrinks the binary to 1.4 MB, about an 80% reduction from the original 6.5 MB.

➜  binary-size git:(main) ✗ go build -ldflags="-s -w" -o server main.go && upx --brute server && ls -lh server
-rwxr-xr-x 1 hxzhouh staff 1.4M Jul 2 14:40 server

How UPX Works

UPX creates a “packed” executable that runs without explicit decompression. It inserts a small decompression stub at the beginning of the file and compresses the remaining sections. At runtime, the stub decompresses the program into memory and then transfers control to the original code. The extra decompression step adds negligible overhead.

Conclusion

By stripping debug information and applying UPX compression (especially with aggressive settings), a Go binary can be reduced from 6.5 MB to roughly 1.4 MB, saving about 80% of space—valuable for large‑scale backend services where binary size matters.

For further details, see the Stack Overflow discussion on reducing Go binary size.

Using println instead of fmt.Println can also avoid pulling in the fmt package, further shrinking the binary.

References

https://go.dev/doc/faq#creating_a_new_language

How Does the Go Compiler Reduce Binary File Size? – https://levelup.gitconnected.com/smart-go-compiler-slimming-bf6a03a7a5dc

UPX – https://upx.github.io/

Stack Overflow: How to reduce Go compiled file size – https://stackoverflow.com/questions/3861634/how-to-reduce-go-compiled-file-size

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

binary sizeCompiler Flagsupxexecutable compression
Radish, Keep Going!
Written by

Radish, Keep Going!

Personal sharing

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.