5

why does the Go compiler produce big binary files?

For example after compiling following code, I get a 1.8 MB exec file

package main
import "fmt"
func main(){
   fmt.Println("Hello World")
}

I tested the above code on both Ubuntu and Win7 and result is the same!

I also wrote a bigger program with +70 lines of code and the resulting binary file was surprisingly again 1.8 MB (actually there were a few bytes difference). I'm wondering what is dumped into the binary file by the Go compiler.

Rick-777
  • 9,714
  • 5
  • 34
  • 50
sepisoad
  • 2,201
  • 5
  • 26
  • 37
  • possible duplicate of [How to reduce compiled file size?](http://stackoverflow.com/questions/3861634/how-to-reduce-compiled-file-size) – kostix Jun 10 '14 at 09:31

1 Answers1

13

Why is my trivial program such a large binary? (from the Go Programming Language FAQ):

The linkers in the gc tool chain (5l, 6l, and 8l) do static linking. 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 1.2 MB, but that includes more powerful run-time support.

Community
  • 1
  • 1
user2864740
  • 60,010
  • 15
  • 145
  • 220
  • 5
    Implied: since the 2MB comes from linking in a big lump of runtime/standard library, binary size isn't proportional to how much code you write--if you write 10x or 100x Hello World, you generally won't get a 18MB or 180MB binary. – twotwotwo Jun 09 '14 at 18:44
  • 1
    @twotwotwo, then is there a version of go optimized for embedded systems? – sepisoad Jun 09 '14 at 19:25
  • 3
    If your target device can't take a 2MB binary, you probably can't use Go. If you build with `-compiler gccgo`, it'll push some of the standard library into a `libgo` shared by all the Go binaries on the device, but regardless, your program needs that code and it's going to take up space somewhere (just like, say, Java and C# need their runtimes installed on the target machine). – twotwotwo Jun 09 '14 at 19:41
  • 1
    Also please note that it's gotten much better in Go 1.3. – OneOfOne Jun 09 '14 at 21:03
  • Even with Go 1.7, the "Hello World" still isn't under a meg… – Geremia Sep 03 '16 at 23:08
  • Go compilers don't support dynamic linking? – Geremia Sep 03 '16 at 23:10
  • @Geremia The Go runtime/core is bundled into the binary *executable*. Different Go libraries (including parts the program functionality, see http://stackoverflow.com/a/30488222/2864740) can be dynamically linked; it is the executable package itself that Go packages up with the minimal runtime. – user2864740 Sep 06 '16 at 03:55
  • @Geremia WIth some flags (-s and -w) to the linker, you get under a meg (but then don't have debugging info)! See: https://blog.filippo.io/shrink-your-go-binaries-with-this-one-weird-trick – Samuel Lampa Nov 19 '16 at 14:10