iota constant in Go

Linearly increasing or decreasing constant values can be declared using “iota”.

For example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
func main() {
    const (
        monday    = 0
        tuesday   = 1
        wednesday = 2
        thursday  = 3
        friday    = 4
        saturday  = 5
        sunday    = 6
    )
}

This can be simplified as:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
func main() {
    const (
        monday    = iota // = 0
        tuesday          // = 1
        wednesday        // = 2
        thursday         // = 3
        friday           // = 4
        saturday         // = 5
        sunday           // = 6
    )
}

iota can be used in expression.

1
2
3
4
5
6
7
func main() {
    const (
        Summer   = (iota + 1) * 3  // = 3
        Monsoon                    // = 6
        Winter                     // = 9
    )
}