Sobes.tech
Junior — Middle

How is the work of multiple goroutines implemented and managed in the application?

sobes.tech AI

Answer from AI

In Go, goroutines are lightweight threads managed by the Go runtime, not the operating system. When an application creates many goroutines, the Go runtime schedules their execution on a limited number of system threads (M), using an M:N model (M system threads for N goroutines).

The Go scheduler distributes goroutines across threads, switching contexts very quickly and efficiently. This allows thousands or even millions of goroutines to run with low memory and time overhead.

Example of creating a goroutine:

func sayHello() {
    fmt.Println("Hello from goroutine")
}

func main() {
    go sayHello() // start goroutine
    time.Sleep(time.Second) // give the goroutine time to execute
}
How is the work of multiple goroutines implemented… - sobes.tech