Go运行时如何可能用Go语言编写的?

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

How is it possible for the Go runtime to be written in Go?

问题

我阅读到从Go 1.4开始,Go运行时是用Go语言编写的(而不是用C语言)。这是如何可能的呢?如果Go程序在运行时之上运行,而运行时又是一个Go程序,那么运行时是在自身之上运行吗?

英文:

I read that from Go 1.4, the Go runtime is written in Go itself (rather than in C).

How is this possible? If Go programs run on top of the runtime, and the runtime is a Go program, does the runtime run on top of itself?

答案1

得分: 6

简而言之:小心使用。

详细来说:unsafe包允许你进行指针算术和任意类型的转换,这在实现Go的垃圾回收器时是必需的。你可以像在普通的Go代码中一样,通过使用栈或静态分配的数据来避免在垃圾回收的Go代码中使用垃圾回收器。下面的链接提到,主流的Go编译器通过一个未记录的选项在运行时强制执行这一点。一些汇编代码片段允许你进行系统调用,这使你可以做任何事情,从创建进程到打开文件。

更详细和权威的解释请参考Ian Lance Taylor(Go团队成员)在golang-nuts论坛上的帖子

英文:

In short: carefully.

In long: The unsafe package lets you do pointer arithmetic and arbitrary casts, which you need to implement go's gc. You avoid using the gc in the gc go code just like you do in normal go code: by using stack- or static-allocated data. The link below mentions that the mainline go compiler enforces this in the runtime via an undocumented option. Some assembly bits let you make syscalls, which lets you do everything from spawning processes to opening files.

In longer and more authoritativer: see Ian Lance Taylor (of the go team)'s post on golang-nuts.

huangapple
  • 本文由 发表于 2016年1月13日 08:16:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/34756262.html
匿名

发表评论

匿名网友

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

确定