[Golang]iota identifier

Reference: https://github.com/golang/go/wiki/Iota
iota is used to declare incremental constants, each constant block will start iota with 0, then each constant within the block will increment the iota. Iota resets back to 0 if it is declared on another constant block.

package main

import "fmt"

func main() {
	const (
		// iota identifier increase by 1 with each constant object.
		a = iota //0
		b = iota //1
		c = iota //2
		d = 10
		e = 15
		f = iota //5
	)

	const (
		no, false, off = iota, iota, iota
		yes, true, on
	)

	const (
		_ = iota //the first constant starts with 0, hence ignore.
		ten = iota * 10 // 1 * 10
		twenty = iota * 10 // 2 * 10
		thirty = iota * 10 // 3 * 10
	)
	
	fmt.Printf("a is %d, b is %d, c is %d, d is %d, e is %d and f is %d", a,b,c,d,e,f)
	fmt.Println("\n=======================================")
	fmt.Printf("no is %b, false is %b, off is %b\n", no, false, off)
	fmt.Printf("yes is %b, true is %b and on is %b", yes, true, on)
	fmt.Println("\n=======================================")
	fmt.Printf("ten is %d, twenty is %d and thirty is %d", ten, twenty, thirty)
}

iota2

Advertisement

One thought on “[Golang]iota identifier

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s