iota用于const表达式中,用于快速给常量进行赋值。iota代表了const声明块的行索引(下标从0开始)。
const块中每一行在go中表示为:
ValueSpec struct {
Doc *CommentGroup // associated documentation; or nil
Names []*Ident // value names (len(Names) > 0)
Type Expr
Values []Expr // initial values; or nil
Comment *CommentGroup // line comments; or nil
}编译时期构建常量的伪代码可以表示为:
for iota, spec := range ValueSpecs {
for i,name := range spec.Names {
obj := NewConst(name, iota...) //此处将iota传入,用于构造常量
...
}
}所以const表达式中每一行对应的iota的值就很清除了。