Go Reference ยท Keywords and Statements
The iota constant generator
Reference for Go iota: how it counts inside const groups, skipping values, bit flags with shifts, byte-size units, String methods and common pitfalls.
iota is a predeclared identifier that is only meaningful inside a const declaration. In each parenthesized const group it starts at 0 on the first line and increases by 1 for every following line (every "constant specification"). Combined with the rule that a line without an expression repeats the previous one, it gives you enumerations with almost no typing.
const ( Red = iota // 0 Green // 1 Blue // 2 )
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.
Syntax
const ( Name1 = iota Name2 Name3 = expression using iota )
| Rule | Description |
|---|---|
| Start | iota is 0 in the first line of a const group. |
| Step | It increases by 1 for each following constant specification, whether or not the line uses it. |
| Repetition | A line with no type and expression repeats the previous line's type and expression, with the new iota. |
| Reset | Each const declaration starts again from 0. |
| Multiple names | Names on the same line share one iota value: a, b = iota, iota + 10. |
Errors and edge cases:
- Using
iotaoutside aconstdeclaration is an error:cannot use iota outside constant declaration. - It is an untyped integer constant, so it takes the type of the surrounding typed expression (
Color(iota)orRead Perm = 1 << iota). - The blank identifier
_consumes a value, which is how you skip numbers. - Overflow of the declared type is a compile error: a ninth
Perm = 1 << iotaconstant in auint8enumeration fails (1 << 8 does not fit).
Usage notes
- Start at 1 when zero should mean "unset". The zero value of a variable is 0, so
_ = iotafirst (oriota + 1) keeps an uninitialized enum distinguishable from a valid one. - Add a
String()method sofmtprints names instead of numbers. Thestringertool can generate it. - Bit flags combine with
|, test with&, clear with&^. Use an unsigned type. - Do not persist or send iota numbers in files or APIs when the order of the list may change; inserting a constant in the middle renumbers everything after it. Assign explicit numbers for stored values.
- Expressions can be anything constant:
iota * 10,1 << iota,iota + 'a', or a call to constant built-ins. - Not a type: Go has no
enumkeyword. The enumeration is a named type plus a group of constants, and nothing stops you from converting any integer to the type.
Related
const,type,switch- Lessons: Constants, Custom types