# 恐慌：sync：负的 WaitGroup 计数器

- **ID:** `go/negative-waitgroup-counter`
- **领域:** go
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

调用 Done() 的次数多于 Add() 的次数，导致计数器减到负数。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 1.0 | active | — | — |
| 1.21 | active | — | — |

## 解决方案

1. **Ensure Add() is called exactly once per goroutine that will call Done()** (95% 成功率)
   ```
   var wg sync.WaitGroup
for i := 0; i < n; i++ {
    wg.Add(1)
    go func() { defer wg.Done(); work() }()
}
wg.Wait()
   ```
2. **Use defer wg.Done() immediately after wg.Add() in the goroutine** (90% 成功率)
   ```
   go func() { defer wg.Done(); ... }()
   ```

## 无效尝试

- **Increasing Add() count arbitrarily** — If Done() is called extra times, the counter still goes negative. (85% 失败率)
- **Ignoring the panic and continuing** — Panic stops execution; ignoring it is not possible. (100% 失败率)
