go runtime_error ai_generated true

panic: 标志重复定义:-verbose

panic: flag redefined: -verbose

ID: go/flag-redefined-panic

其他格式: JSON · Markdown 中文 · English
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.

generic

官方文档

https://pkg.go.dev/flag#Var

解决方案

  1. Ensure each flag name is unique across the entire program: use descriptive prefixes like `-server-port` and `-client-port` instead of just `-port`.
  2. Use separate flag sets for different packages and parse each set independently: `fs := flag.NewFlagSet("myapp", flag.ExitOnError); fs.String("verbose", "", "desc")`

无效尝试

常见但无效的做法:

  1. 70% 失败

    The default set is still used; panics persist unless all flags are on a custom set.

  2. 80% 失败

    The flag name string is what matters; variable name change doesn't affect registration.