This is a created article in which the information may have evolved or changed.
package mainimport ( "fmt")type BitFlag intconst ( // iota为0,1左移0位 = 1 Active BitFlag = 1 << iota // Send <=> Active <=> 1 << iota,此时iota为1,1左移1位 = 2 Send // Receive <=> Send <=> 1 << iota,此时iota为2,1左移2位 = 4 Receive)func main() { fmt.Println(Active, Send, Receive)}
Iota is at compile time, the compiler is dynamically replaced by the position of the iota in the code and the Const keyword.
package mainimport ( "fmt")const ( //e=0,f=0,g=0 e, f, g = iota, iota, iota)func main() { fmt.Println(e, f, g)}
Iota can be understood as the row index of a const statement
package mainimport ( "fmt")func main() { fmt.Println(iota)}
Compilation error: Undefined:iota.
Iota are pre-declared identifiers, but only in const constant declarations.
How do I feel iota this thing is a go bastard, can only be locked in a place, unlike True/false and other brothers, cannot access it.