Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Go produce big binary files for small programs?

Tags:

binary

go

size

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.

like image 524
sepisoad Avatar asked Dec 11 '22 05:12

sepisoad


1 Answers

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.

like image 176
user2864740 Avatar answered Dec 20 '22 19:12

user2864740