# panic：sync：WaitGroup 计数器为负数

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

## 根因

wg.Done() 调用次数多于 wg.Add()，通常是重复减计数或循环中 Done 未匹配 Add。

## 版本兼容性

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

## 解决方案

1. **** (95% 成功率)
   ```
   wg.Add(1)
go func() {
    defer wg.Done()
    work()
}()
   ```
2. **** (92% 成功率)
   ```
   g, ctx := errgroup.WithContext(context.Background())
g.Go(func() error { return work(ctx) })
return g.Wait()
   ```

## 无效尝试

- **** — Panic is thrown inside sync.WaitGroup internal state; recover in the goroutine may catch it but leaves the counter corrupted. (80% 失败率)
- **** — Masks the bug; wg.Wait() then blocks forever because counter never reaches zero. (85% 失败率)
