Go: g0, Special Goroutine
ℹ️ This article is based on Go 1.13.
All the goroutines created in Go are under the management of an internal scheduler. The Go scheduler tries to give running time to all goroutines and keep all CPU busy with running goroutines when the current ones are blocked or terminated. It actually runs as a special goroutine.
Scheduling goroutine
Go limits the number of OS thread running thanks to GOMAXPROCS
variable simultaneously. That means Go has to schedule and manage goroutines on each of the running threads. This role is delegated to a special goroutine, called g0
, that is the first goroutine created for each OS thread:
Then, it will schedule ready goroutines to run on the threads.
For more information about the P
, M
, G
model, I suggest you read my article “Go: Goroutine, OS Thread and CPU Management.”
To better understand how scheduling works on g0
, let’s review the usage of the channels. Here is when a goroutine blocks on sending on channels:
ch := make(chan int)
[...]
ch <- v
When blocking on channels, the current goroutine will be parked, i.e., be in waiting mode and not been pushed in any goroutines queues: