user200783
user200783

Reputation: 14346

How is it possible for the Go runtime to be written in 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?

Upvotes: 5

Views: 2077

Answers (1)

W. Cybriwsky
W. Cybriwsky

Reputation: 571

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.

Upvotes: 7

Related Questions