blob: 6126c51c25bc702abdb4acb148f39dd7a9d9f891 [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
Dorka5c8aec72015-11-07 19:37:11 +01005The values of iota is reset to 0 whenever the reserved word ` const ` appears in the source (i.e. each const block) and increments by one after each [ConstSpec](https://golang.org/ref/spec#ConstSpec) e.g. each Line. This can be combined with the constant shorthand (leaving out everything after the constant name) to very concisely define related constants.
Andrew Gerrand5bc444d2014-12-10 11:35:11 +11006
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```