Sobes.tech
Junior — Middle

Kuidas toimub mitme goroutine töö koordineerimine Go-s?

sobes.tech AI

Vastus AI-lt

In Go, coordination of multiple goroutines' work is usually done using channels and the sync package.

  • Channels allow goroutines to exchange data and synchronize execution, blocking the sender or receiver until they are ready.
  • sync.WaitGroup is used to wait for a group of goroutines to finish.
  • Mutex and other primitives from sync provide safe access to shared resources.

Example of using a channel for synchronization:

ch := make(chan int)
func() {
    // Perform work
    ch <- 1 // signal completion
}()

<-ch // wait for the signal

Thus, channels and synchronization primitives enable efficient coordination of parallel tasks.