Skip to content
Tutorials Center
My learning

Topics

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.

Go
const (
	Red = iota // 0
	Green      // 1
	Blue       // 2
)

Try it

Go Enum with a String method

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.

Go Flags, sizes and skipping

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

Go
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 iota outside a const declaration 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) or Read 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 << iota constant in a uint8 enumeration 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 _ = iota first (or iota + 1) keeps an uninitialized enum distinguishable from a valid one.
  • Add a String() method so fmt prints names instead of numbers. The stringer tool 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 enum keyword. The enumeration is a named type plus a group of constants, and nothing stops you from converting any integer to the type.