dsfsdfsd34324 2019-03-21 14:23
浏览 429


I have a question about golangci-lint. The linter returns me for something like:

type outputFormat string

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

an error like:

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

But what's the problem about it? In https://go101.org/article/constants-and-variables.html they describe in chapter Autocomplete in constant declarations that incomplete constants will be recognized and filled.

I did not find any reference which tells me to avoid using incomplete constant definitions.

Can somebody explain to me, what's the matter behind that?

  • 写回答

1条回答 默认 最新

  • douxing5199 2019-03-21 14:26

    As explained in staticcheck's 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

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?



  • ¥100 读取 IMU BNO080 数据
  • ¥15 基于RTKLIB框架写的精密单点定位-AR
  • ¥15 PFENet的预训练权重
  • ¥15 程序哪有错误怎么改?
  • ¥15 交换机和交换机之间的链路带宽以及主机带宽的理解
  • ¥15 ai创想家对战模式代码
  • ¥15 集合A由3个2行4列二维数组构成,从集合A中任意取一个二维数组元素、如果该二维数组元素的对应列位置的上、下两数都是奇数,而且仅有2个列是奇数/奇数,则该数组有意义,并放入集合B中打印输出。
  • ¥15 电信IPV6 无法外网访问吗
  • ¥15 有偿求效果比较好的遥感影像匹配的c++代码
  • ¥15 博主,你好,我下载了你的智能网联汽车辅助驾驶安全信息检测系统,现在不会运行,可以教我吗,