gpt4 book ai didi

golangci-lint 常量显式类型

转载 作者:IT王子 更新时间:2023-10-29 01:51:47 37 4
gpt4 key购买 nike

我有一个关于 golangci-lint 的问题。linter 返回我的东西是这样的:

type outputFormat string

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

错误如:

only the first constant in this group has an explicit type (staticcheck)
formatNone outputFormat = ""

但这有什么问题呢?在 https://go101.org/article/constants-and-variables.html他们在常量声明中的自动完成一章中描述了不完整的常量将被识别和填充。

我没有找到任何引用资料告诉我要避免使用不完整的常量定义。

谁能给我解释一下,这是怎么回事?

最佳答案

staticcheck 中所述的 documentation for that check :

In a constant declaration such as the following:

const (
First byte = 1
Second = 2
)

the constant Second does not have the same type as the constant First. This construct shouldn't be confused with

const (
First byte = iota
Second
)

where First and Second do indeed have the same type. The type is only passed on when no explicit value is assigned to the constant.

When declaring enumerations with explicit values it is therefore important not to write

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

This discrepancy in types can cause various confusing behaviors and bugs.

关于golangci-lint 常量显式类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55282615/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com