Skip to content

Latest commit

 

History

History
65 lines (49 loc) · 1.01 KB

4.07.md

File metadata and controls

65 lines (49 loc) · 1.01 KB

Section 4.07: Iota

In the Go Specification, near the end of the section on constant declarations, it assigns the value iota to a const.

Iota is the predeclared identifier.

package main

import (
	"fmt"
)

const (
	a = iota
	b = iota
	c = iota
)

func main() {
	fmt.Println(a)
	fmt.Println(b)
	fmt.Println(c)
	
	fmt.Printf("%T\n", a)
	fmt.Printf("%T\n", b)
	fmt.Printf("%T\n", c)
}

playground

You can see this gives us the auto incrementing values, 0, 1, 2. A shorthand for declaring auto incremented constant values is to just set the first one to iota

package main

import (
	"fmt"
)

const (
	a = iota
	b
	c
)

const (
	d = iota
	e
	f
)

func main() {
	fmt.Println(a)
	fmt.Println(b)
	fmt.Println(c)
	fmt.Println(d)
	fmt.Println(e)
	fmt.Println(f)
}

You can see the auto incrementing restarts in each new group of const declarations.