What is 'iota' in Go?
What is iota
in Go? Let’s find out, in this quick introduction.
iota
and enums
We often define constants in Go to enumerate a list of possible values that something could take (sometimes called an “enum” for short). For example, suppose we’re writing an astronomy program, and we want to be able to distinguish between different kinds of night-sky objects:
const (
= 0
Planet = 1
Star = 2
Comet = 3
Galaxy // ...and so on
)
The specific numbers 0, 1, 2… aren’t important here. They’re just arbitrary values we can compare things against:
if object.Kind == Planet {
// it's a planet!
}
Using iota
with const
It’s hard to maintain a long list of numbers like this by hand, though. That’s where iota
comes in. It’s a kind of magical, ever-increasing value we can use to assign arbitrary numbers to constants:
const (
= iota // 0
Planet // 1
Star // 2
Comet // 3
Galaxy // ...
)
We’re effectively assigning iota
to every constant in the list, but Go lets us omit all but the first “= iota
”. Very handy!
Starting iota
at 1
And we can use iota
in more complicated expressions, too. For example, if we want the successive values to start at 1 instead of 0:
const (
= iota+1
January
February
March// ...
)
Here, the value of January
will be 1, February
will be 2, and so on.
iota
skips comments
By the way, you can put comments or blank lines in your constant block, and it won’t affect the values of iota
:
const (
// fermions
= iota // 0
Electron // 1
Neutron // 2
Proton
// bosons
// 3
Photon // 4
Gluon )
iota
, flags, and bitmasks
Another common use for iota
is creating values for bitmasks. A bitmask is something you can compare against a binary number to see if a particular bit in that number is 1 or 0. When particular bit values are used in this way, they’re sometimes called flags, and the number containing them is called a bitfield (well, well).
For example:
const (
= 1 << iota // 1
Bit0 // 2
Bit1 // 4
Bit2 // ...
)
The <<
operator shifts the bits of a number to the left by the given number of places. Since iota
increases by one for each successive constant, that means the shift factor increases by one too, so the values assigned to the constants keep on doubling.
Now we can do a bitwise comparison of these mask values against some number we’re interested in:
:= 0b00000010
v if v&Bit1 != 0 {
.Println("Looks like bit 1 is set!")
fmt}
// Output:
// Looks like bit 1 is set!
Now you know all about iota
in Go! Have fun with it.
Want to learn more? Check out my book introducing Go to beginners, For the Love of Go.