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?
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?
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.
© 2022 - 2024 — McMap. All rights reserved.