为什么Go编译版本相对较大?

huangapple go评论97阅读模式
英文:

why is the Go compiled version is relatively huge?

问题

最近我安装了Go并尝试了hello world的例子。

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

> $ go build hello.go

返回一个1.2Mb大小的hello二进制文件。相对于一个简单的hello world程序来说,这个文件大小相对较大。是否有特定的原因导致文件大小较大?是因为导入了"fmt"吗?

英文:

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" ?

答案1

得分: 5

这是一个Go FAQ

为什么我的简单程序会生成如此大的二进制文件?

gc工具链中的链接器(5l、6l和8l)进行静态链接。
所以所有的Go二进制文件都包含了Go运行时,以及支持动态类型检查、反射甚至panic时的堆栈跟踪所需的运行时类型信息。

在Linux上,使用gcc编译和静态链接的一个简单的C“hello, world”程序大约为750 kB,其中包括了printf的实现。
使用fmt.Printf的等效的Go程序大约为1.2 MB,但它包含了更强大的运行时支持。

英文:

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.

答案2

得分: 1

是的,“fmt”包是其中一个原因。它还会导入其他包。但即使不使用“fmt”,整个运行时也会静态链接到Go二进制文件中。而Go的运行时并不简单 - 它包括调度器/ goroutine与操作系统线程管理器,分割堆栈分配器,垃圾收集器和垃圾收集器友好的内存分配器,也是C线程友好的,信号处理程序和堆栈跟踪生成器,...

英文:

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, ...

huangapple
  • 本文由 发表于 2013年3月11日 02:20:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/15325804.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定