blob: f15514c7fcb859b56ab2d46ef3fdebf260fc0010 [file] [log] [blame] [view]
Andrew Gerrand5bc444d2014-12-10 11:35:11 +11001# Summary
2
swyman407ce0e2015-03-27 10:33:56 -07003Go's ` iota ` identifier is used in ` const ` declarations to simplify definitions of incrementing numbers. Because it can be used in expressions, it provides a generality beyond that of simple enumerations.
Andrew Gerrand5bc444d2014-12-10 11:35:11 +11004
5The values of iota start at zero within each const block and increment by one each time it is seen. This can be combined with the constant shorthand (leaving out everything after the constant name) to very concisely define related constants.
6
7Iota: http://golang.org/doc/go_spec.html#Iota
8
9Constant declarations: http://golang.org/doc/go_spec.html#Constant_declarations
10
11# Examples
12
13The official spec has two great examples:
14
15http://golang.org/doc/go_spec.html#Iota
16
17Here's one from Effective Go:
18
19```
20type ByteSize float64
Dave Day0d6986a2014-12-10 15:02:18 +110021
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110022const (
Dave Day0d6986a2014-12-10 15:02:18 +110023 _ = iota // ignore first value by assigning to blank identifier
24 KB ByteSize = 1 << (10 * iota)
25 MB
26 GB
27 TB
28 PB
29 EB
30 ZB
31 YB
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110032)
33```