# http：写入的内容超过声明的 Content-Length

- **ID:** `go/http-header-write-after-write`
- **领域:** go
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

处理程序显式设置了 Content-Length 但写入的字节数超出声明，或中间件重写了 body。

## 版本兼容性

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

## 解决方案

1. **** (90% 成功率)
   ```
   Buffer the response with bytes.Buffer, then set Content-Length to buf.Len() before w.Write.
   ```
2. **** (92% 成功率)
   ```
   Do not set Content-Length manually; let net/http chunk or compute it.
   ```

## 无效尝试

- **** — Clients hang waiting for the remaining bytes. (85% 失败率)
- **** — Headers already sent; no effect. (90% 失败率)
