TIL: wrapping my head around Go’s “iota”
TIL (“Today I learned”) are shorter, less-researched posts that I typically write to help organize my thoughts and solidify things I have learned while working. I post them here as a personal archive and of course for others to possibly benefit from.
Iota
A few days ago, I happened upon the iota
identifier in Go.
I remembered seeing it before, but it’s still pretty rare, so I looked up the definition of iota
in the documentation:
const iota = 0 // Untyped int.
iota is a predeclared identifier representing the untyped integer ordinal number of the current const specification in a (usually parenthesized) const declaration. It is zero-indexed.
¿Qué?
Upon reading this (several times over actually) I must confess I still didn’t really understand what iota
was or did. And while the language specification is more explicit and has examples, it is not much clearer:
Within a constant declaration, the predeclared identifier iota represents successive untyped integer constants. Its value is the index of the respective ConstSpec in that constant declaration, starting at zero. It can be used to construct a set of related constants