# panic: sync: negative WaitGroup counter

- **ID:** `go/goroutine-waitgroup-negative-counter`
- **Domain:** go
- **Category:** runtime_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## Root Cause

Calling Done() more times than Add(), causing WaitGroup counter to go negative.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| 1.0 | active | — | — |
| 1.22 | active | — | — |

## Workarounds

1. **Ensure Add is called exactly once per goroutine before Done** (95% success)
   ```
   var wg sync.WaitGroup
for i := 0; i < 10; i++ {
    wg.Add(1)
    go func() {
        defer wg.Done()
        // work
    }()
}
wg.Wait()
   ```
2. **Use defer wg.Done() immediately after wg.Add()** (98% success)
   ```
   wg.Add(1)
go func() {
    defer wg.Done()
    // work
}()
   ```

## Dead Ends

- **Using recover() to catch panic and continue** — Panic indicates misuse; recovering doesn't fix the counter imbalance. (90% fail)
- **Adding extra Add() calls to compensate** — Race condition may cause unpredictable counter values; proper accounting is needed. (80% fail)
