go
runtime_error
ai_generated
true
http: superfluous response.WriteHeader call from main.handler (server.go:42)
ID: go/http-superfluous-writeheader
80%Fix Rate
87%Confidence
0Evidence
2024-06-11First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 1.18 | active | — | — | — |
| 1.21 | active | — | — | — |
Root Cause
WriteHeader is called more than once, or Write is called before WriteHeader. Only the first status code is sent.
generic中文
WriteHeader 被调用多次,或在 WriteHeader 之前调用了 Write。只有第一个状态码会被发送。
Workarounds
-
90% success
if !wroteHeader { w.WriteHeader(http.StatusOK); wroteHeader = true } json.NewEncoder(w).Encode(v) -
88% success
Return early after writing an error status; never write body after WriteHeader without checking.
Dead Ends
Common approaches that don't work:
-
80% fail
Hides a real bug; the wrong status code may be sent to clients.
-
70% fail
Does not fix the underlying control flow.