iota is useful for creating incremental constants, but there are also unsuitable application scenarios. This article introduces several examples suitable for using itoa, and also reminds some scenarios to be careful.
Basic iota usage
First, let's start with its basic usage:
const ( StarHyperGiant = iota StarSuperGiant StarBrightGiant StarGiant StarSubGiant StarDwarf StarSubDwarf StarWhiteDwarf StarRedDwarf StarBrownDwarf )
The above code defines a set of constant types. The initial value is assigned with the itoa identifier. It tells the Go compiler that the first constant starts from 0 and the subsequent constants are incremented by one in turn
When const appears before the block is declared, the counter is reset to 0. A set of enumerations in the following code will be re assigned from zero to four:
const ( StarHyperGiant = iota StarSuperGiant StarBrightGiant StarGiant StarSubGiant ) const ( StarDwarf = iota StarSubDwarf StarWhiteDwarf StarRedDwarf StarBrownDwarf )
Modify the default enumeration type
Enumeration constants are typeless integers by default. We can also use other numeric types to override the default type. See the example:
const ( StarDwarf byte = iota StarSubDwarf StarWhiteDwarf StarRedDwarf StarBrownDwarf )
You can also specify any numeric type that can represent integer or floating point types. Each constant in the above code is declared as a byte type.
Use itoa in expressions
When itoa appears in the expression, the implementation mechanism is the same as above. The compiler applies an expression to each continuous itoa increment value, and the following code assigns an even value to the enumeration member:
const ( StarHyperGiant = 2. 0*iota StarSuperGiant StarBrightGiant StarGiant StarSubGiant )
The output results are as follows:
StarHyperGiant = 0 [float64] StarSuperGiant = 2 [float64] StarBrightGiant = 4 [float64] StarGiant = 6 [float64] StarSubGiant = 8 [float64]
Skip enumeration values
For enumeration constant assignment, we can also skip some values. For an empty identifier, the value is skipped. The following example skips 0 and 64:
_ = iota // value 0 StarHyperGiant = 1 << iota StarSuperGiant StarBrightGiant StarGiant StarSubGiant _ // value 64 StarDwarf StarSubDwarf StarWhiteDwarf StarRedDwarf StarBrownDwarf
Because we skipped 0, the first assignment constant starts with 1. The result of expression 1 < < iota is 1 < < 1 = 2. The sixth position returns 64, but the value is skipped. The output result is:
StarHyperGiant = 2 StarSuperGiant = 4 StarBrightGiant = 8 StarGiant = 16 StarSubGiant = 32 StarDwarf = 128 StarSubDwarf = 256 StarWhiteDwarf = 512 StarRedDwarf = 1024 StarBrownDwarf = 2048
Let's take another example:
const ( _ = 1 << (iota * 10) // ignore the first value KB // decimal: 1024 -> binary 00000000000000000000010000000000 MB // decimal: 1048576 -> binary 00000000000100000000000000000000 GB // decimal: 1073741824 -> binary 01000000000000000000000000000000 )
The output result is:
KB = 1024 MB = 1048576 GB = 1073741824
More crazy usage
We can also use itoa pairing to assign values to constants on the same line, and we can also use underscores to skip the values in the pairing:
const ( tomato, apple int = iota + 1, iota + 2 orange, chevy ford, _ )
Pairing uses itoa to calculate each row:
tomato = 1, apple = 2 orange = 2, chevy = 3 ford = 3
Like all programming languages, you can do this, but it doesn't mean you should. It is recommended that production environments do not write such confusing code.
This paper introduces several application scenarios of Golang's itoa notation through examples.