Constants and Enumerates
Constants
Use keyword const
to define constants in Go
1 | func consts() { |
Note, unlike in other languages, consts in Go is not in upper case, since upper case means public in Go.
Enumerate
Enumerate is a special type of const
, e.g.
1 | func enums() { |
We could use built-in iota
to simplify defining auto-incremental consts
1 | func enums() { |
It will print the results:
1 | 0 1 2 3 4 |
iota
can be used in some interesting applications, e.g. enumerate bit, KB, MB etc.
1 | func enums() { |
1 | 1 1024 1048576 1073741824 1099511627776 1125899906842624 |