gpt4 book ai didi

binary - 为什么 Go 会为小程序生成大二进制文件?

转载 作者:IT王子 更新时间:2023-10-29 00:35:11 37 4
gpt4 key购买 nike

为什么 Go 编译器会生成大二进制文件?

例如编译以下代码后,我得到一个 1.8 MB 的可执行文件

package main
import "fmt"
func main(){
fmt.Println("Hello World")
}

我在 UbuntuWin7 上测试了上面的代码,结果是一样的!

我还用 +70 行代码编写了一个更大的程序,结果二进制文件令人惊讶地再次达到 1.8 MB(实际上有几个字节的差异)。我想知道 Go 编译器将什么转储到二进制文件中。

最佳答案

Why is my trivial program such a large binary? (来自 Go 编程语言常见问题解答):

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.

关于binary - 为什么 Go 会为小程序生成大二进制文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24126398/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com