SoFunction
Updated on 2025-03-05

Understand the usage and principles of iota in Golang in one article

Preface

We knowiotaIt is a constant counter of the Go language, and can only be used in constants.constUsed in expressions, inconstThe keyword will be reset to0constEach new row of constants declare that the iota value is increased by 1 (iotaIt can be understood as a row index in a const statement block). Using iota can simplify the definition of constants, but its rules must be firmly grasped, otherwise misunderstandings may occur in our development. This article tries to comprehensively summarize its usage and implementation principles. Friends who need it can refer to the following content, hoping it will be helpful to everyone.

Use of iota

iota will be reset to 0 when the const keyword appears

iotaOnly used in constant expressions,iotaexistconstThe keyword will be reset to0. differentconstDefinition blocks do not interfere with each other.

//The const keyword will be reset to 0 when it appearsconst (
    a = iota  //0
    b         //1
)
//Different const definition blocks do not interfere with each otherconst (
    c = iota //0
)

Count by row

Const is added to each row of constant declaration, and iota counts once. It can be used as an index in the const statement, and is often used to define enumeration data.

const (
    n1 = iota //0
    n2 		  //1
    n3 		  //2
    n4 		  //3
)

All comment lines and blank lines are ignored

All comment lines and blank lines will be cleared first during the compilation period, so blank lines are not counted.

const (
    a = iota  //0
    
    b		  //1
    
    //This line is a comment    c         //2
)

Jump value placeholder

If a value is not required, you can use the placeholder“_”, it is not a blank line, it will count and play a role in jumping value.

const (
    a = iota //0
    _
    b        //2
)

Multiple iota

Multiple iotas appear in the same const block, and they will only count according to the number of rows, and will not count again.

const (
    a = iota     // a=0
    b = iota     // b=1
    c = iota     // c=2
  )

Multiple iota in one row

Multiple iotas in a row, counting them separately.

const (
    a, b = iota, iota // a=0,b=0
    c, d              // c=1,d=1
)

First line of jumping

The count will be performed when you start to jump to the queue.

const (
    a = 100 //  a=100
    b = iota // b=1
    c = iota // c=2
    d        // d=3
)

In the middle of the line

The middle line will be counted.

const (
    a = iota // a=0
    b = 100  // b=100
    c = iota // c=2
    d        // d=3
)

Constant definitions that multiplex the expressions on the previous line

const (
   a = iota       //  iota = 0 
   b = 1 + iota  // iota = 1
   c           // iota = 2
)

Implementation principle

iota definition

The definition of iota source code in the Go language code base is located in a built-in filego/src/builtin/middle:

const iota = 0 // Untyped 

Here is a constant identifier, whose value is 0; iota is just a simple integer 0, why can it be self-incremented as a constant counter? Let's take a look at the implementation of const.

const

Each row in the const block is described in Go using the spec data structure, and the spec declares as follows:

ValueSpec struct {
 Doc *CommentGroup // associated documentation; or nil
 Names []*Ident // value names (len(Names) > 0)
 Type Expr // value type; or nil
 Values []Expr // initial values; or nil
 Comment *CommentGroup // line comments; or nil
}

There is a slice in this structure, and the constants defined in a row are stored in this slice. If N constants are defined in a row, the length of the slice is N.

A const block is actually a spec-type slice that represents multiple rows in a const.

The pseudo-algorithm when constructing constants during compilation is as follows:

for iota, spec := range ValueSpecs {
 for i, name := range  {
 obj := NewConst(name, iota...) // Pass iota in here to construct constants ...
 }
}

iota is actually an index that iterates over the const block. Even if iota is used multiple times in each row, its value will not increase.

This is the article about understanding the usage and principles of iota in Golang. For more related Golang iota content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!