iota makes it easy to declare sequentially growing numeric constants:
https://codeeval.dev/gist/819d209619b595557942a06facb506f7
iota sets the value of Low to 0 and instructs the compiler that the following constants have increasing numeric values.
iotaIota can be very useful when creating a bitmask. For instance, to represent the state of a network connection which may be secure, authenticated, and/or ready, we might create a bitmask like the following:
https://codeeval.dev/gist/67206141b4b2985a6df7be09c2bfb031
The value of iota is still incremented for every entry in a constant list even if iota is not used:
https://codeeval.dev/gist/b52473024d89db16329ca485584db867
It will also be incremented even if no constant is created at all, meaning the empty identifier can be used to skip values entirely:
https://codeeval.dev/gist/fff5fb28ddfa5b3da361c3d06146bad1
iota in an expression listBecause iota is incremented after each ConstSpec, values within the same expression list will have the same value for iota:
https://codeeval.dev/gist/4c56666f684275ae882dc898e1b9269d
iota in an expressioniota can be used in expressions, so it can also be used to assign values other than simple incrementing integers starting from zero. To create constants for SI units, use this example from Effective Go: