-1

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?

nikoss
  • 3,254
  • 2
  • 26
  • 40

1 Answers1

7

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.

joshlf
  • 21,822
  • 11
  • 69
  • 96
  • 2
    And the docs aren't update for later than go1.7, which compiles that program to around 1.4M (and ~1M if you link with `-w` and `-s`) – JimB Oct 19 '16 at 20:52