How to shrink a Go executable

The short version: build with go build -trimpath -ldflags="-s -w". On my machine that makes a Go binary about a third smaller, with no change in how it runs. Compressing the result with UPX can shrink it further, but that comes with trade-offs I’ll go through below.

If you don’t care about the size of your executables, that’s perfectly fine. Disks are big and networks are fast. But if you ship a command-line tool that thousands of people download, or you’re squeezing a program onto a small device, a few megabytes add up.

Why are Go binaries so large?

A Go binary is self-contained. It includes the Go runtime (the garbage collector and the goroutine scheduler), every package your program uses, and the information Go needs to print stack traces when something panics. Nothing has to be installed on the machine that runs it, and that’s the price you pay for it. By default, the binary also contains a symbol table and DWARF debugging information, which are only useful to debuggers and similar tools. Those parts you can drop.

The test programs

I measured two programs: Hello, world, and a minimal web server, since net/http pulls in a lot more code. Each one lives in its own folder with a go.mod (see Build and run a Go program if that’s new to you).

package main

import "fmt"

func main() {
	fmt.Println("Hello, world!")
}Code language: Go (go)
Hello, world!Code language: plaintext (plaintext)
package main

import (
	"fmt"
	"log"
	"net/http"
)

func main() {
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprintln(w, "Hello, web!")
	})
	log.Fatal(http.ListenAndServe(":8080", nil))
}Code language: Go (go)

Strip the symbol table and debug info

The -ldflags option passes flags to the Go linker, the part of the toolchain that assembles the final binary. Two of them matter here (from go tool link -help):

  • -s: disable symbol table
  • -w: disable DWARF generation
go build -ldflags="-s -w"Code language: Bash (bash)

Since Go 1.22, -s also implies -w on all platforms, so -ldflags=-s alone gives the same result. In my measurements it produced byte-for-byte the same size. I still write -s -w because everyone recognizes it, and it says what I mean.

An older version of this post said this only works when you build a module, not a single file. That’s not true (anymore): go build -ldflags="-s -w" main.go works fine.

What you give up

Panics still show function names, file names and line numbers, because Go keeps that information separately from the symbol table. Here’s a stripped binary that panics:

panic: boom

goroutine 1 [running]:
main.main()
	example.com/p/main.go:4 +0x2cCode language: plaintext (plaintext)

What you lose is everything that reads DWARF or the symbol table: stepping through the binary with a debugger such as Delve, and tools like go tool nm, which listed 2,420 symbols for my normal Hello, world binary and only 58 for the stripped one. For release builds that’s usually fine. You can always build an unstripped copy from the same source when you need to debug.

Remove file paths with -trimpath

-trimpath is a go build flag rather than a linker flag. It removes all file system paths from the binary. Without it, the trace above would have shown the full path to the source file on my disk (/Users/...), including my user name. With it, file names start with the module path, as in example.com/p/main.go.

It saves only a few kilobytes, as you’ll see in the table. I use it for release builds anyway, because it keeps details of my machine out of the binary and makes builds on different machines more alike. Combined:

go build -trimpath -ldflags="-s -w"Code language: Bash (bash)

The numbers

I measured with Go 1.27.3 on macOS 26.6 on an Apple silicon Mac (darwin/arm64). Sizes are in megabytes (1 MB = 1,000,000 bytes). Your numbers will differ with the Go version, operating system, processor and, of course, your code, but the proportions should be similar.

BuildHello, worldWeb server
go build2.43 MB8.25 MB
-trimpath2.41 MB8.22 MB
-ldflags="-w"1.69 MB5.99 MB
-ldflags="-s -w" (same as -s)1.59 MB5.58 MB
-trimpath -ldflags="-s -w"1.57 MB5.54 MB

Stripping takes 35% off Hello, world and 33% off the web server. Cross-compiling Hello, world from the same Mac showed the same pattern: linux/amd64 went from 2.35 MB to 1.52 MB, and windows/amd64 from 2.50 MB to 1.66 MB.

Downloads are usually compressed, so I also gzipped the Hello, world binaries (gzip -9). The normal build compressed to 1.43 MB and the stripped one to 0.68 MB. The linker already stores DWARF data compressed by default (its -compressdwarf flag), so gzip can’t squeeze that part much further. That’s why stripping helps a compressed download even more than the raw file size suggests.

Compress the executable with UPX

UPX (the Ultimate Packer for eXecutables) is a free, open-source tool that isn’t specific to Go. It compresses an executable and adds a small piece of code that unpacks it in memory when the program starts. You run it on a finished binary, for example upx --best myprogram. The UPX documentation says it typically shrinks programs by 50 to 70%.

I didn’t have UPX installed while updating this post, so I have no measurements of my own to share. These are the trade-offs to weigh before you use it:

  • Startup cost. The program is decompressed every time it starts. For a long-running server that’s noise, but for a small tool that runs many times in a script it adds up. Measure it for your program.
  • Antivirus false positives. Malware often uses packers to hide, so some antivirus products are suspicious of packed executables. If your users are on Windows, a warning or quarantined download costs you more than a few megabytes ever would.
  • Platform support. The UPX README names Windows programs and Linux executables among the formats it supports. If you ship for macOS or other systems, check the UPX documentation and test the packed binary on each platform. Code signing also has to happen after packing, not before.
  • Smaller gain than it looks. If you distribute your program in a .zip or .tar.gz, the download is already compressed, and compressing twice doesn’t buy much.

The UPX README also warns that it inherits the security context of the files it handles, so only use it on files you trust.

When not to bother

  • Tools for yourself or your team. Nobody will notice 1 MB.
  • Programs you debug in production, or anything you might attach a debugger to. Keep the debug info, or at least keep an unstripped build of every release.
  • Servers and containers. A Go binary in a minimal container image is already small, and the image layers get compressed for transfer anyway. The -s -w flags are harmless here, but UPX rarely pays off.

Where I do use it: release builds of command-line tools that people download. There, -trimpath -ldflags="-s -w" is a free win, and I leave UPX out.

Key takeaways

  • go build -trimpath -ldflags="-s -w" made my binaries about a third smaller and changes nothing at runtime.
  • Since Go 1.22, -s implies -w; panics in stripped binaries still show function names and line numbers.
  • -trimpath saves little space but keeps your local file paths out of the binary.
  • UPX compresses further at the cost of startup time, possible antivirus warnings and platform caveats. Measure before you adopt it.