0

I recently installed go and was trying out hello world example.

package main
import "fmt"
func main() {
    fmt.Printf("hello, world\n")
}

$ go build hello.go

returns hello binary file which is 1.2Mb size. Thats comparatively huge for just a hello world program. Any particular reason on why is the file size large ? Is it because of importing "fmt" ?

Rahul
  • 11,129
  • 17
  • 63
  • 76

2 Answers2

5

This is a Go FAQ

Why is my trivial program such a large binary?

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.

Nick Craig-Wood
  • 52,955
  • 12
  • 126
  • 132
1

Yes, package "fmt" is one of the reasons. It also in turn imports other packages. But even without using "fmt", there's the whole runtime statically linked into a Go binary. And Go's runtime is not a simple one - it includes for instance a scheduler/goroutine vs OS thread manager, split stack allocator, the garbage collector and garbage collector friendly memory allocator which is also C threads friendly, signal handlers and stack trace generator, ...

zzzz
  • 87,403
  • 16
  • 175
  • 139