Sobes.tech
Junior

What other methods of goroutine synchronization exist in Go besides channels?

sobes.tech AI

Answer from AI

In addition to channels, in Go, you can use the following for goroutine synchronization:

  • sync.WaitGroup — allows waiting for a group of goroutines to finish.
  • sync.Mutex — a mutex for protecting shared resources from concurrent access.
  • sync.RWMutex — a mutex with separate read and write locks.
  • sync.Cond — a condition variable for notifying goroutines about an event.
  • atomic operations from the sync/atomic package — for atomic operations on variables without locks.

Example of using WaitGroup:

var wg sync.WaitGroup
wg.Add(1)
go func() {
    defer wg.Done()
    // goroutine work
}()
wg.Wait() // wait for all goroutines to finish
What other methods of goroutine synchronization exist… - sobes.tech