Iota is a constant counter for the Golang language and can only be used in expressions of constants.
Iota will be reset to 0 (before the first line of the const interior) when the const keyword appears, and each new line of constant declarations in the const will make the Iota count once (iota can be understood as the row index in the Const statement block).
Using iota can simplify the definition and is useful when defining enumerations.
Examples are as follows:
1. Iota can only be used in expressions of constants.
fmt.Println(iota)
Compilation Error: Undefined:iota
2, each const appears, will let iota initialized to 0.
const a = iota // a=0 const ( b = iota //b=0 c //c=1 )
3. Custom type
Self-growing constants often contain a custom enumeration type that allows you to rely on the compiler to complete the self-increment settings.
type Stereotype intconst ( TypicalNoob Stereotype = iota // 0 TypicalHipster // 1 TypicalUnixWizard // 2 TypicalStartupFounder // 3 )
4. Values that can be skipped
Imagine that you are dealing with consumer audio output. The audio may not have any output whatsoever, or it may be mono, stereo, or surround sound.
This may have some potential logic definitions without any output of 0, mono as 1, stereo 2, and the value is provided by the number of channels.
So what value do you give Dolby 5.1 surround stereo.
On the one hand, it has 6 channel outputs, but on the other hand, only 5 channels are full-bandwidth channels (hence the 5.1 designation-where. 1 represents a low-frequency effect channel).
Anyway, we don't want to add to 3 simply.
We can use the underscore to skip the unwanted values.
type AudioOutput intconst ( OutMute AudioOutput = iota // 0 OutMono // 1 OutStereo // 2 _ _ OutSurround // 5 )
5, bit mask expression
type Allergen intconst ( IgEggs Allergen = 1 << iota // 1 << 0 which is 00000001 IgChocolate // 1 << 1 which is 00000010 IgNuts // 1 << 2 which is 00000100 IgStrawberries // 1 << 3 which is 00001000 IgShellfish // 1 << 4 which is 00010000 )
The job is because when you have only one noon line in a const group, it will use the growing iota to get the previous expression and apply it. In the Go language spec, this is known as the implicit repetition of the last non-empty expression list.
If you are allergic to eggs, chocolate and seafood, flip these bits to the "on" position (mapping bits from left to right). Then you will get a bit value of 00010011, which corresponds to the decimal 19.
fmt.Println(IgEggs | IgChocolate | IgShellfish)// output: // 19
6. Define the order of magnitude
type ByteSize float64const ( _ = iota // ignore first value by assigning to blank identifier KB ByteSize = 1 << (10 * iota) // 1 << (10*1) MB // 1 << (10*2) GB // 1 << (10*3) TB // 1 << (10*4) PB // 1 << (10*5) EB // 1 << (10*6) ZB // 1 << (10*7) YB // 1 << (10*8))
7, defined in the case of a row
const ( Apple, Banana = iota + 1, iota + 2 Cherimoya, Durian Elderberry, Fig)iota 在下一行增长,而不是立即取得它的引用。// Apple: 1 // Banana: 2 // Cherimoya: 2 // Durian: 3 // Elderberry: 3 // Fig: 4
8, the middle queue
const ( i = iota j = 3.14 k = iota l )
So the printed result is i=0,j=3.14,k=2,l=3.
9. Binary Mobile
const ( bit00 uint32 = 5 << iota //bit00=5 bit01 //bit01=10 bit02 //bit02=20 ) fmt.Printf("bit00 = %d, bit01 = %d, bit02 = %d\n", bit00, bit01, bit02) //bit00 = 5, bit01 = 10, bit02 = 20