Golangci-lint常数显式类型



我有一个关于golangci-lint的问题。Linter返回我的东西:

type outputFormat string
const (
    formatNone outputFormat = ""
    formatText              = "TEXT"
    formatJSON              = "JSON"
)

错误:

仅此组中的第一个常数具有显式类型 (静态检查)
formatnone outputformat ="

但是有什么问题?在https://go101.org/article/constants-and-variables.html中,他们在convent 中描述了常数声明中的自动完整,将识别和填充不完整的常数。

我没有找到任何参考,告诉我要避免使用不完整的常数定义。

有人可以向我解释,这是怎么回事?

如staticcheck的文档中所述:

在常数声明中,例如以下内容:

const (
     First byte = 1
     Second     = 2
)

常数Second do 具有与常数First相同的类型。此结构不应与

混淆
const (
      First byte = iota
      Second
)

FirstSecond确实具有相同的类型。仅当未分配给常数时,该类型才会传递。

当声明具有显式值的枚举时,重要的是不要写

const (
      EnumFirst EnumType = 1
      EnumSecond         = 2
      EnumThird          = 3
)

这种类型的差异可能会导致各种令人困惑的行为和错误。

在我的情况下,我解决了所有枚举项目的类型

type Verb string
const (
    DEL  Verb = "DEL"
    POST Verb = "POST"
    GET  Verb = "GET"
)

当我被puttig动词仅在第一个枚举(del)上时,我得到了你的同一问题

最新更新