# HTTP：在已关闭的响应体上执行读取操作

- **ID:** `go/http-response-body-not-closed`
- **领域:** go
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 90%

## 根因

在 http.Response.Body 已经被关闭后尝试从中读取数据，通常是由于过早关闭或多个协程共享该 body 导致。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| Go 1.18 | active | — | — |
| Go 1.19 | active | — | — |
| Go 1.20 | active | — | — |
| Go 1.21 | active | — | — |
| Go 1.22 | active | — | — |

## 解决方案

1. ```
   在检查错误后始终 defer resp.Body.Close()，并在 defer 作用域内读取 body
   ```
2. ```
   如果多个协程需要读取同一个 body，使用 sync.Once 或互斥锁
   ```

## 无效尝试

- **** — Closing the body before reading makes the data unavailable; the body must be read first, then closed. (80% 失败率)
- **** — If the body is already closed, ReadAll will panic; always check errors and ensure body is open. (70% 失败率)
