go
build_error
ai_generated
true
http: /api/v1/users 的重复注册
http: multiple registrations for /api/v1/users
ID: go/http-multiple-registrations
80%修复率
88%置信度
0证据数
2024-05-10首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 1.19 | active | — | — | — |
| 1.22 | active | — | — | — |
根因分析
相同的 URL 模式多次注册到 http.Handle 或 http.HandleFunc,导致 panic。
English
The same URL pattern is registered multiple times with http.Handle or http.HandleFunc, causing a panic.
解决方案
-
90% 成功率
Check if pattern is already registered before adding: if _, exists := mux.Handlers()[pattern]; !exists { mux.Handle(pattern, handler) } -
85% 成功率
Use a single handler with method-based routing: http.HandleFunc("/api/v1/users", func(w http.ResponseWriter, r *http.Request) { switch r.Method { case "GET": ... case "POST": ... } })
无效尝试
常见但无效的做法:
-
100% 失败
Go panics on duplicate registration; this is not allowed.
-
50% 失败
Overriding can lead to unexpected behavior and is not idiomatic.