# panic：sync：WaitGroup 在上一次 Wait 返回前被重用

- **ID:** `go/panic-waitgroup-misuse-add-inside-goroutine`
- **领域:** go
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

在 wg.Wait 返回后又调用 wg.Add，或 Add 在 goroutine 内与 Wait 竞态调用。

## 版本兼容性

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

## 解决方案

1. **** (95% 成功率)
   ```
   wg.Add(len(items))
for _, it := range items {
    go func(x Item) { defer wg.Done(); process(x) }(it)
}
wg.Wait()
   ```
2. **** (93% 成功率)
   ```
   g := new(errgroup.Group)
for _, it := range items {
    it := it
    g.Go(func() error { return process(it) })
}
return g.Wait()
   ```

## 无效尝试

- **** — Races with Wait; Wait may observe zero counter and return early. (90% 失败率)
- **** — WaitGroup is not designed for reuse until Wait returns; runtime panics. (85% 失败率)
