Home >Backend Development >Golang >Why Are Go Executables So Much Larger Than Expected?
When compiling a trivial "Hello World" program in Go, you may be surprised to find its executable is much larger than expected. This issue has been addressed in the Go FAQ: "Why is my trivial program such a large binary?"
The explanation lies in Go's linking process. Go uses static linking, meaning the Go runtime and all necessary type information for dynamic type checks, reflection, and panic handling are included in the final executable. This approach provides robust runtime support, but it comes at the expense of file size.
Comparing Go to C, a statically linked "Hello World" C program is typically around 750 kB, including printf implementation. In contrast, a Go equivalent using fmt.Printf takes up 1.9 MB due to its extensive runtime support and type information.
However, the size remains consistent when you add more fmt.Println() calls to your Go program. This is because the required libraries and runtime are already included in the executable. The addition of new text only adds a minimal amount of data.
Understanding this behavior is essential for optimizing Go executables. While Go's runtime support provides many conveniences, it comes at the cost of file size. By tailoring your code to use only the necessary features, you can reduce the bloat of your executables.
The above is the detailed content of Why Are Go Executables So Much Larger Than Expected?. For more information, please follow other related articles on the PHP Chinese website!