The "all goroutines are asleep - deadlock!" error occurs when your program has no runnable goroutines, usually because a channel send or receive is blocked waiting for a partner that never arrives. Check for missing go keywords on goroutines, unbuffered channels where one side is blocked, or logic that prevents a goroutine from ever starting.
// Example: Missing 'go' keyword causes deadlock
func main() {
ch := make(chan int)
// Missing 'go' here blocks main immediately
producer(ch)
fmt.Println(<-ch)
}
func producer(ch chan int) {
ch <- 42
}
// Fix: Add 'go' to run producer concurrently
func main() {
ch := make(chan int)
go producer(ch) // Now runs in background
fmt.Println(<-ch)
}
func producer(ch chan int) {
ch <- 42
}