Why is my trivial program such a large binary?
The linker in the
gc
toolchain creates statically-linked binaries by default. All Go binaries therefore include the Go runtime, 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 usingfmt.Printf
weighs a couple of megabytes, but that includes more powerful run-time support and type and debugging information.A Go program compiled with
gc
can be linked with the-ldflags=-w
flag to disable DWARF generation, removing debugging information from the binary but with no other loss of functionality. This can reduce the binary size substantially.
來源:https://go.dev/doc/faq#Why_is_my_trivial_program_such_a_large_binary
參考:https://stackoverflow.com/questions/28576173/reason-for-huge-size-of-compiled-executable-of-go