英文:
Why does Go produce big binary files for small programs?
问题
为什么Go编译器生成的二进制文件如此大?
例如,编译以下代码后,我得到一个1.8 MB的可执行文件:
package main
import "fmt"
func main(){
fmt.Println("Hello World")
}
我在Ubuntu和Win7上都测试了上述代码,结果是一样的!
我还编写了一个更大的程序,有70多行代码,结果生成的二进制文件仍然是1.8 MB(实际上有几个字节的差异)。我想知道Go编译器将什么内容转储到二进制文件中。
英文:
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.
答案1
得分: 13
为什么我的简单程序会生成如此庞大的二进制文件?
在Go编程语言的常见问题解答中,有这样一段解释:
链接器(gc工具链中的5l、6l和8l)进行静态链接。因此,所有的Go二进制文件都包含了Go运行时,以及支持动态类型检查、反射甚至panic时的堆栈跟踪所需的运行时类型信息。
在Linux上,使用gcc编译和静态链接的简单C语言“hello, world”程序大约为750 kB,其中包括了printf的实现。而使用等效的Go程序并使用fmt.Printf的话,大约为1.2 MB,但其中包含了更强大的运行时支持。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论