nikoss nikoss - 1 month ago 8
C Question

Go binary size in comparison with c

Yesterday I just wanted to compare simple golang

HelloWorld
app with c, go binary was something like
2-3 MB
(just
fmt.Println
) the equivalent c code, however, was just about 20 kb (
printf
). Then I checked system calls both binaries were doing, using
strace
; there was not a huge difference between both so do you have any idea why golang binary is so massive in comparison with c equivalent?

Answer

Go binaries are, by default, statically linked, and also contain more information such as information about types that allows for reflection, type assertions, and so on. To quote the Go FAQ:

The linker in the gc tool chain creates statically-linked binaries by default. All Go binaries therefore include the Go run-time, along with the run-time type information necessary to support dynamic type checks, reflection, and even panic-time stack traces.

A simple C "hello, world" program compiled and linked statically using gcc on Linux is around 750 kB, including an implementation of printf. An equivalent Go program using fmt.Printf is around 2.3 MB, but that includes more powerful run-time support and type information.