Go 运行时怎么可能用 Go 编写?

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

read 从 Go 1.4 开始,Go 运行时间是用 Go 本身编写的(而不是用 C)。

这怎么可能?如果 Go 程序 运行 在 运行time 之上,并且 运行time 是 Go 程序,那么 运行time 运行 是否在其自身之上?

简而言之:小心。

总而言之:unsafe 包可以让你进行指针运算和任意类型转换,你需要实现 go 的 gc。您可以避免像在普通 go 代码中那样在 gc go 代码中使用 gc:通过使用 stack- 或 static-allocated 数据。下面的 link 提到主线 go 编译器通过未记录的选项在运行时强制执行此操作。一些汇编位允许您进行系统调用,这使您可以执行从生成进程到打开文件的所有操作。

更长更权威:见Ian Lance Taylor (of the go team)'s post on golang-nuts