iota在每个const块内独立从0开始计数,按行递增且不受非iota语句影响;需同块定义、显式类型绑定、位运算时用1
Go 里的 iota 看似简单,实则暗藏逻辑细节。写错一行,可能让枚举值错位、类型不匹配、反序列化失败,甚至权限位运算完全失效。下面这 6 种场景,是工程中高频出问题的地方。
很多人误以为 iota 是全局计数器。其实它只在每个 const 块内有效,且每次遇到 const 关键字就重置为 0。
const A = iota // A = 0
const B = iota // B = 0(不是 1)
const (
A = iota // 0
B // 1
)
iota 的自增是按行计数的,与是否赋值无关。只要某行用了 iota,下一行 iota 就 +1;即使中间写了 X = 100,也不会打断计数节奏。
X = 100 会让 iota “暂停”或“重置”const (
Zero = iota // 0
One // 1
Hi = 100 // 不影响 iota 计数
Four // 3(不是 2!因为 iota 已走到第 3 行)
)
_ = iota 显式占位,语义清晰且不易出错const (
_ = iota // 跳过 0
Active // 1
Inactive // 2
)
如果 const 块第一项没声明类型(如 Running = iota),整个块都推导为无类型整数。传给 func f(s Status) 这类带类型的参数时会编译报错。
const (
Running = iota
Paused
)
func f(s Status) { ... }
f(Running) // ❌ cannot use Running (untyped int) as Status value
type State int
const (
Running State = iota
Paused
)
f(Running) // ✅
想表达“读+写”权限组合(Read | Write),必须让每个常量是独立 bit 位(1, 2, 4, 8…)。用 iota 直接递增得到的是 0,1,2,3 —— 完全不能做按位判断。
const (
Read = iota // 0
Write // 1
Delete // 2
)
if perm & Read != 0 { ... } // ❌ 永远为 true(0 & x == 0,1 & x 可能非 0,但逻辑混乱)
const (
Read Permission = 1 Write // 2
Delete // 4
)
裸 int 枚举打印出来就是 2、3,调试困难,API 返回也不友好。更糟的是,新增枚举项后忘了补 switch 分支,String() 返回空字符串,线上难排查。
必须用值接收器(func (s Status) String()),不能用指针接收器
default 分支不可省略,否则新值返回空串
func (s Status) String() string {
switch s {
case Unknown: return "unknown"
case OK: return "ok"
case Error: return "error"
default: return fmt.Sprintf("status(%d)", int(s))
}
}
iota 不传递、不共享。不同文件、不同 const 块、甚至同一文件里两个分开写的 const,都是独立计数。试图靠“顺序”对齐值,注定失败。
文件 A:const X = iota // 0
文件 B:const Y = iota // 0(不是 1)
—— 本想让 Y = 1,结果还是 0
// 比如 HTTP 状态码和业务状态码,绝不混用同一 iota 序列
type HTTPStatus int
const (
_ HTTPStatus = iota
Continue = 100
OK = 200
)
type BizStatus int
const (
Pending BizStatus = iota
Approved
)