Tutorial by Examples

To create a list of constants - assign iota value to each element: const ( a = iota // a = 0 b = iota // b = 1 c = iota // c = 2 ) To create a list of constants in a shortened way - assign iota value to the first element: const ( a = iota // a = 0 b // b = 1 c /...
iota can be used in expressions, so it can also be used to assign values other than simple incrementing integers starting from zero. To create constants for SI units, use this example from Effective Go: type ByteSize float64 const ( _ = iota // ignore first value by assigning to b...
The value of iota is still incremented for every entry in a constant list even if iota is not used: const ( // iota is reset to 0 a = 1 << iota // a == 1 b = 1 << iota // b == 2 c = 3 // c == 3 (iota is not used but still incremented) d = 1 << iota ...
Because iota is incremented after each ConstSpec, values within the same expression list will have the same value for iota: const ( bit0, mask0 = 1 << iota, 1<<iota - 1 // bit0 == 1, mask0 == 0 bit1, mask1 // bit1 == 2, mask1 == 1 _, _ ...
Iota can be very useful when creating a bitmask. For instance, to represent the state of a network connection which may be secure, authenticated, and/or ready, we might create a bitmask like the following: const ( Secure = 1 << iota // 0b001 Authn // 0b010 Ready ...
This is an enumeration for const creation. Go compiler starts iota from 0 and increments by one for each following constant. The value is determined at compile time rather than run time. Because of this we can't apply iota to expressions which are evaluated at run time. Program to use iota in cons...

Page 1 of 1