# Go: fatal error: all goroutines are asleep - deadlock!

**Code:** `all goroutines are asleep - deadlock` · **Category:** Go · **Seen on:** Go

Every goroutine is blocked waiting (on a channel, mutex or WaitGroup), so the program can never continue.

## Meaning

Classic causes: sending on an unbuffered channel with no receiver, ranging over a channel that’s never closed, WaitGroup.Wait with Add counts that never reach zero, or locking a mutex twice.

## Common causes

- Send on unbuffered channel without a receiver goroutine
- range over a channel never closed
- wg.Add/Done mismatch
- Mutex locked twice in the same goroutine

## Quick fix

1. Start the receiver before sending or use a buffered channel
2. close() channels when producers finish
3. Match every Add with Done (defer wg.Done())

## Detailed fix by platform

### Go

1. 

```go
ch := make(chan int)
go func() {
    defer close(ch)
    for i := 0; i < 3; i++ { ch <- i }
}()
for v := range ch { fmt.Println(v) }
```


## How to diagnose

1. **Trace** — goroutine states (chan send, chan receive, semacquire)
2. **Channels** — Who closes them?

---
Source: [WhatIsMyError](https://whatismyerror.com/errors/go/all-goroutines-are-asleep-deadlock)
