go
runtime_error
ai_generated
true
panic: 标志重复定义:-verbose
panic: flag redefined: -verbose
ID: go/flag-redefined-panic
90%修复率
88%置信度
1证据数
2023-11-20首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| go1.20 | active | — | — | — |
| go1.21 | active | — | — | — |
| go1.22 | active | — | — | — |
| go1.23 | active | — | — | — |
根因分析
使用已在同一 flag.CommandLine 集合中注册的标志名称调用 flag.String、flag.Int 或类似函数。
English
Calling flag.String, flag.Int, or similar with a flag name that has already been registered in the same flag.CommandLine set.
官方文档
https://pkg.go.dev/flag#Var解决方案
-
Ensure each flag name is unique across the entire program: use descriptive prefixes like `-server-port` and `-client-port` instead of just `-port`.
-
Use separate flag sets for different packages and parse each set independently: `fs := flag.NewFlagSet("myapp", flag.ExitOnError); fs.String("verbose", "", "desc")`
无效尝试
常见但无效的做法:
-
70% 失败
The default set is still used; panics persist unless all flags are on a custom set.
-
80% 失败
The flag name string is what matters; variable name change doesn't affect registration.