Go · Stage 10 – Concurrency · Lesson 60 of 64

Race Conditions and Deadlocks

Recognize two common concurrency failures.

Go code

ch := make(chan int)
ch <- 1
Line-by-line explanation
  1. An unbuffered channel is created.
  2. Main tries to send.
  3. No other goroutine can receive.
  4. The program deadlocks.
Expected output
fatal error: all goroutines are asleep - deadlock!

This is the expected result.

Use channels, mutexes, ownership, and cancellation deliberately. Run tests with go test -race to help find data races.