Junior — Middle
Millistel juhtudel lõpetab üks gorutina täitmise ja annab juhtimise üle teisele?
sobes.tech AI
Vastus AI-lt
In Go, a goroutine stops executing and transfers control to another goroutine in the following cases:
- When the goroutine explicitly calls a function that blocks it (e.g., reading from an empty channel).
- When using the keyword
runtime.Gosched(), which voluntarily yields the processor to other goroutines. - When the goroutine finishes its function (returns from the function), it stops executing.
The Go scheduler automatically switches execution between goroutines, ensuring concurrency. For example, when blocked on input/output operations or waiting for data from a channel, the current goroutine is suspended, and control is transferred to another.