Dev Skills

Constants and iota

Values fixed at compile time, and how Go does enums without an enum keyword.

Topic 5 of 101 · 10 min to read · 45 min to build

By devskills editorial team · Last updated

What a constant is

A constant is a value fixed when the program is compiled and never changed afterwards. It is not a variable you promise not to touch; the compiler enforces it. Try to assign to one and the program will not build.

Use a constant for values that are true for the whole program: a limit, an app name, a tax rate.

Constants

const.go
const Pi = 3.14159
const Greeting = "hello"
const (
MaxUploadMB = 10
AppName = "snapnotes"
)
// A constant with no type written adapts to where it is used.
const ten = 10
var f float64 = ten // fine: becomes 10.0
var i int64 = ten // also fine
// A constant with a type written does not adapt
const typed int32 = 100
var x int64 = typed // COMPILE ERROR
A constant without a written type is called **untyped**. Go treats it as "just the number 10" until you put it somewhere, then it takes that type. That is why the same `ten` fits in both a `float64` and an `int64`.

iota — numbering without typing numbers

iota.go
const (
Pending = iota // 0
Active // 1
Suspended // 2
Deleted // 3
)
// iota is just a number, so you can do arithmetic with it
const (
_ = iota // skip 0
Small = iota * 10 // 10
Medium // 20
Large // 30
)
`iota` starts at 0 in each `const` block and goes up by one per line. A line with no `=` repeats the previous expression with the next `iota`. Go has no `enum` keyword; this is how you number a set of choices. In **Methods and embedding** you will give them a type and make them print as words.

A real enum: its own type, a String method, and a safe zero value

Plain iota constants are untyped integers, so any int can be passed where you meant a state. Giving them a named type (type State int) lets the compiler reject a stray number, and a String() method makes fmt print the name instead of the number.

Start the block with an Unknown (or Invalid) value. Every Go variable starts at its zero value, so a State that was never set is 0. If 0 were Pending, a forgotten assignment would look like a real state and nobody would notice.

1 << iota gives each constant its own bit, which is the Go way to write a set of flags: combine them with |, test with &, and clear one with &^ (AND NOT, an operator many languages do not have).

enum.go
package main
import "fmt"
// State gets its own type, so a plain int cannot be passed by mistake.
type State int
const (
Unknown State = iota // 0, the zero value: "not set yet"
Pending // 1
Active // 2
Suspended // 3
)
// String makes fmt print the name instead of the number.
func (s State) String() string {
switch s {
case Unknown:
return "unknown"
case Pending:
return "pending"
case Active:
return "active"
case Suspended:
return "suspended"
}
return fmt.Sprintf("State(%d)", int(s))
}
// Perm is a set of bit flags: each constant is its own bit.
type Perm uint8
const (
Read Perm = 1 << iota // 1
Write // 2
Delete // 4
)
func main() {
var s State // never assigned
fmt.Println(s, Active, State(9))
p := Read | Write
fmt.Println(p&Write != 0, p&Delete != 0) // true false
p &^= Write // clear the Write bit
fmt.Println(p == Read) // true
}

Pitfalls

  • Inserting a line renumbers everything after it. If the numbers are stored in a database or sent over the wire, adding Archived in the middle silently changes what 3 means. Append new values at the end, or write the numbers out explicitly once they are persisted.
  • `iota` resets to 0 in every `const` block and counts lines, not names: a blank _ line still uses up a number.
  • A typed constant does not adapt. const Limit int32 = 100 cannot be assigned to an int64 without a conversion; an untyped const Limit = 100 can.
  • Untyped constants are exact until used. const big = 1 << 62 is fine; var x int32 = big does not compile, because the value does not fit. The check happens at compile time, not as a runtime overflow.
  • Only basic values can be constants: numbers, strings, booleans and runes. A slice, map or struct has to be a var.

Cost and the Go angle

Constants cost nothing at run time: the compiler substitutes the value wherever it is used, and a switch over a small typed enum compiles to a few comparisons. The price of the Go approach is that nothing stops State(9) from existing, which is why the String method above has a fallback, and why code that receives a state from outside should validate it.

For a larger set, go generate with the stringer tool writes the String method for you; it is in the golang.org/x/tools module.

Check you can now
  • Write a const block that numbers a set of states with iota.
  • Explain why an untyped constant fits a float64 and an int64 but a typed one does not.
  • Predict the values iota produces in a block with a skipped first line and arithmetic.
The short version
  • A constant is fixed at compile time. The compiler enforces it; it is not a promise.
  • A constant without a written type adapts to where it is used; a typed one does not.
  • iota counts up from 0 through a const block, and you can do maths with it.
  • Go has no enum keyword. A const block with iota numbers a set of choices.
Remember: There are no constant slices, maps or structs. Those are var.
Build this before moving on
Write a const block of four order states with iota, then print each one and check one with an if.
Then try these in your own editor
  1. 1Use iota to generate the even numbers 2, 4, 6.
  2. 2Declare const ten = 10 and assign it to both a float64 and an int64.
  3. 3Give the same constant an explicit type and watch the second assignment stop compiling.
Check yourselfTake the Getting started test