Binary size and startup time
A Go binary contains the program, the runtime, the garbage collector, the scheduler and the packages it imports, and it starts without loading a runtime library. That is why a hello-world binary is a couple of megabytes and starts in a few milliseconds, and why a static binary fits a scratch container. The previous lesson stamped the version into the binary; this one looks at what else is in it.
Four builds, four sizes
The four builds differ only in flags. -trimpath removes the absolute paths of the build machine from the binary, which makes builds reproducible and slightly smaller. -s -w removes the symbol table and the debug information. CGO_ENABLED=0 drops the C library dependency, which matters for the size of the container image more than for the binary itself.
package main
import (
"fmt"
"os"
"sort"
"strings"
)
func main() {
words := strings.Fields("the quick brown fox jumps over the lazy dog")
sort.Strings(words)
fmt.Fprintln(os.Stdout, strings.Join(words, " "))
}
go build -o app . && stat -c 'plain: %s bytes' app
plain: 2423986 bytes
go build -trimpath -ldflags='-s -w' -o app-small . && stat -c 'trimmed+strip:%s bytes' app-small
trimmed+strip:1601698 bytes
CGO_ENABLED=0 go build -trimpath -ldflags='-s -w' -o app-static . && stat -c 'static: %s bytes' app-static
static: 1601698 bytes
go build -o app-cgo . && file -b app-cgo | cut -d, -f1-2 && file -b app-static | cut -d, -f1-2
ELF 64-bit LSB executable, x86-64
ELF 64-bit LSB executable, x86-64
The last line shows the difference the C library makes: the default build is a dynamically linked executable, the CGO_ENABLED=0 one is static. A static binary runs on any Linux with the same architecture, which is what makes FROM scratch images possible.
Counting what is inside
go tool nm lists the symbols in a binary, and wc -l counts them. A stripped binary has none, which is the direct evidence that the debugger will have nothing to read. The build info survives stripping because it is a data section, so a stripped release binary can still be identified with go version -m.
go build -o app . && go tool nm app | wc -l
2726
go build -ldflags='-s -w' -o app-stripped . && go tool nm app-stripped | wc -l
0
reading app-stripped: no symbol section
reading app-stripped: no symbols
go version -m app-stripped | head -3
app-stripped: go1.26.10
path example.com/app
mod example.com/app (devel)
The plain binary has thousands of symbols and the stripped one reports zero, which is the number the linker's -s removed. The build info still names the module and the Go version, so the trade is symbols and DWARF for size, not identity.
Startup time
A Go program initialises the runtime, runs the package initialisers and then main, and that is the whole startup: there is no bytecode verification and no shared library to load in a static build. A command-line tool that runs a hundred times in a script therefore notices a difference between a Go binary and an interpreter, and a server notices nothing at all because it starts once. Measure with time on a loop of a hundred runs rather than trusting a single number: the first run pays for the page cache and the rest do not.
Exercises
- Build the same program with and without -trimpath and compare the two binaries byte for byte. Are they identical, and why does the build id differ?
- Add a dependency that imports net/http and compare the binary size with the hello-world one. Which package pulls in the most?
- Time a hundred runs of a Go tool and of a shell script that does the same work. Which part of the difference is startup and which is the work?
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does a Go binary contain besides the program?
Answer
The runtime, the collector, the scheduler and the imported packages
The runtime is linked in, which is why the binary is large and the startup short.
-
What does -trimpath remove?
Answer
The absolute paths of the build machine
It makes builds reproducible across machines.
-
What does -s -w remove?
Answer
The symbol table and the debug information
The binary gets smaller and cannot be debugged with symbols.
-
What does CGO_ENABLED=0 change about the executable?
Answer
It links no C library, so the binary is static
Static binaries run in scratch containers.
-
What does go tool nm list?
Answer
The symbols in a binary
A stripped binary lists none.
-
Why does the build info survive -s -w?
Answer
It is a data section, not a symbol table entry
go version -m reads that section from a stripped binary.
-
What makes a Go program start quickly?
Answer
The runtime is linked in and there is no interpreter or shared library to load
Startup is runtime initialisation and main.
-
Why measure with a loop instead of one run?
Answer
The first run pays for the page cache and the noise dominates one measurement
A hundred runs separate startup from noise.
-
Which build is the usual release image?
Answer
A static, trimmed, stripped binary
Small and portable, with go version -m still available.
-
What grows when a program imports net/http?
Answer
The binary, because the package and its dependencies are linked in
Imports are code, and unused code is not linked, but http pulls a lot.
The code on this page was run with go 1.26.10 on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev