Skip to content

The iota Keyword in Go

Iota is used to create a sequence of values.

Table of Contents

Basics of IOTA Incrementation

When the iota keyword is used in a constant declaration, it will increment by 1 for each following identifier (variable).

By default, iota starts at 0 (zero).

E.g.,

const (
    a = iota
    b
    c
)
This will assign a := 0, b := 1, and c := 2.

Skipping Incrementations

Iota's incrementations can be skipped using a blank identifier (an underscore _).

const (
    a = iota
    _
    b
    c
)
This will output:
0
2
3
The underscore (_) will make iota increment, but it will not be saved into any value.

Starting from a Specific Value

You can start iota at a specific value. Do this by adding a value after the iota keyword:

const (
    a = iota + 10
    b
    c
)
This will output:
10
11
12

IOTA Usage Example

For example, this program:

package main

import "fmt"

const (
    a = iota
    b
    c
    _
    d
)

func main() {
    var vals []int = []int{a, b, c, d}
    for i := 0; i < len(vals); i++ {
        fmt.Printf("%d\n", vals[i])
    }
    fmt.Printf("End of incrementations of constants.\n")
}
This will output:
0
1
2
4
End of incrementations of constants.

Enum in Golang

IOTA provides an automated way to create a enum (enumeration) in Golang.

type Size uint8

const (
        small Size = iota
        medium
        large
        extraLarge
      )

func main() {
    fmt.Println(small)
        fmt.Println(medium)
        fmt.Println(large)
        fmt.Println(extraLarge)
}
Output:
0
1
2
3

  • We created a new type: type Size uint8
  • Then we declared some const of type Size.
  • The first constant, small, is set to iota so it will be set to zero
    small Size = iota
    

And so:

fmt.Println(small)      >> outputs 0  
fmt.Println(medium)     >> outputs 1
fmt.Println(large)      >> outputs 2
fmt.Println(extraLarge) >> outputs 3

Enums Without IOTA

Without IOTA we'd have to explicitly set the values of each of the enum value

type Size uint8

const (
    small      Size = 0
    medium     Size = 1
    large      Size = 2
    extraLarge Size = 3
)

func main() {
    fmt.Println(small)
    fmt.Println(medium)
    fmt.Println(large)
    fmt.Println(extraLarge)
}
Output:
0
1
2
3