Constants, iota and typed constants

A constant in Go is a value the compiler knows. It has no address, it cannot be assigned to, and it can be used where a type is needed, such as the length of an array. A const declaration is evaluated at compile time, which is why an expression that cannot be folded is an error.

iota

Inside a const block, iota is the index of the line, counted from zero and reset by every block. It is the language's enumeration: a named type plus one constant per line, so the values are typed and the type has a name in signatures.

package main import "fmt" type Weekday int const ( Sunday Weekday = iota Monday Tuesday Wednesday Thursday Friday Saturday ) var names = [...]string{ "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", } func main() { fmt.Println(names[Monday], int(Monday)) fmt.Println(names[Friday], int(Friday)) fmt.Println("days:", int(Saturday)+1) }

go run iota.go

Monday 1
Friday 5
days: 7

Weekday is a named type over int, and the index of an array accepts it because it is an integer type. The values are 0 to 6, which is what int(Monday) shows; the name is only in the source. A String method would turn the number back into a name, and the lesson on methods adds one.

Bit flags and skipped values

A _ line skips a value, and an expression with iota can compute one: 1 << (10 * iota) is the familiar ladder of byte sizes. The same trick makes a set of flags, where each constant is one bit and | combines them.

package main import "fmt" const ( _ = iota KB = 1 << (10 * iota) MB GB TB ) const ( Read = 1 << iota // 1 Write // 2 Exec // 4 ) func main() { fmt.Println(KB, MB, GB, TB) fmt.Println("read|write =", Read|Write) fmt.Println("exec set in read|write:", (Read|Write)&Exec != 0) }

go run sizes.go

1024 1048576 1073741824 1099511627776
read|write = 3
exec set in read|write: false

Powers of two are the reason the ladder skips zero: the first line is the blank identifier, so KB starts at 1024 rather than at 1. Flags work the same way, and the lesson on operators reads a set with & and tests a bit with &^.

Untyped constants

A constant declared without a type has arbitrary precision until it is used. Pi is written with 36 digits, and the compiler rounds it to the type of the variable it initialises: float32 keeps about seven digits, float64 about sixteen. The same rule makes Big exact until it meets a type that cannot hold it.

package main import "fmt" const Pi = 3.14159265358979323846264338327950288 const Big = 1 << 62 func main() { var f32 float32 = Pi var f64 float64 = Pi fmt.Printf("%.6f\n", float32(Pi)) fmt.Printf("%.15f\n", f64) fmt.Println("float32 keeps", f32, "as float64", float64(f32)) fmt.Println("Big:", Big) fmt.Println("Big/3:", Big/3) }

go run untyped.go

3.141593
3.141592653589793
float32 keeps 3.1415927 as float64 3.1415927410125732
Big: 4611686018427387904
Big/3: 1537228672809129301

This is also why const Big = 1 << 62 can be divided by 3 at compile time without an overflow: the arithmetic happens before the value gets a type. Assigning it to an int32 would be the error, and the error names the overflow, as the next section shows.

When a constant does not fit

A typed constant must fit its type at compile time. The program below asks an int8 to hold 200, and the compiler refuses the declaration rather than wrapping the value at run time.

package main func main() { const big int8 = 200 _ = big }

go build overflow.go

# command-line-arguments
./overflow.go:4:19: cannot use 200 (untyped int constant) as int8 value in constant declaration (overflows)

The message names the constant, the type and the reason. A variable of the same type would wrap silently, which is the subject of the numbers lesson: the compiler protects the constant, and the programmer protects the variable.

Exercises

  1. Add a String method to Weekday that returns names[d]. Which values of the type are then safe to print?
  2. Declare flags for read, write and execute with iota, then print the set Read|Exec and test it with &.
  3. Assign Big from the program above to an int32. What does the compiler say, and why is the message about a constant?

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. When is a constant evaluated?

    Answer

    At compile time

    A constant is folded by the compiler, which is why it may set an array length.

  2. What does iota hold inside a const block?

    Answer

    The index of the line in the block, from zero

    iota counts the lines of the block and resets at every new block.

  3. What is the value of MB in the block _ = iota; KB = 1 << (10 * iota); MB?

    Answer

    1048576

    MB is on the third line, so iota is 2 and the shift is 20 bits.

  4. What happens when a typed constant does not fit its type?

    Answer

    The compiler reports the overflow

    Constants are checked at compile time, unlike variables, which wrap.

  5. What does this program print? package main import "fmt" const ( a = iota b c ) func main() { fmt.Println(a, b, c) }

    Answer

    0 1 2

    Each line of the block increments iota, starting from zero.

  6. What does this program print? package main import "fmt" const ( _ = iota KB = 1 << (10 * iota) MB ) func main() { fmt.Println(KB, MB) }

    Answer

    1024 1048576

    KB is the second line with iota 1, and MB is the third with iota 2.

  7. What does this program print? package main import "fmt" const Pi = 3.14159265358979 func main() { var f float32 = Pi fmt.Println(f) }

    Answer

    3.1415927

    A float32 keeps about seven digits, and Println prints the shortest form that round-trips.

  8. What does const Big = 1 << 62; fmt.Println(Big / 3) print?

    Answer

    a number computed at compile time

    The constant is untyped and exact until it meets a type, so the division is exact.

  9. Can a constant have an address, as in &Pi?

    Answer

    No: constants are not variables

    A constant is a compile-time value, not an object in memory, so it cannot be addressed.

  10. Which declaration makes a set of bit flags?

    Answer

    1 << iota on each line of a const block

    Each flag gets its own bit, and | combines them.

The code on this page was run with go 1.26.10 on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev