Junior — Middle
What happens to a goroutine when it is blocked on executing some process or operation?
sobes.tech AI
Answer from AI
When a goroutine in Go blocks on an operation (for example, reading from a channel, waiting for synchronization, or I/O), it is suspended, and the Go scheduler releases the system thread to run other goroutines. This allows efficient use of system resources and scaling of parallelism.
That is, a blocking operation does not block the entire OS thread, only the specific goroutine. The scheduler switches to other goroutines that are ready to run.
Example:
ch := make(chan int)
go func() {
val := <-ch // goroutine blocks until it receives a value
fmt.Println(val)
}()
ch <- 42 // unblocks the goroutine
In this example, the goroutine blocks on reading from the channel, but the scheduler allows other goroutines to work while it waits.