
Without any explicit initialization, the zero-value gets assigned, thus indicating the state Created. In the example above, I have done so by distinguishing between states Created and Initialized. When defining an enumeration in Go, you should keep this in mind. All variables and struct fields that are not explicitly initialized to some value will get their type’s zero-value. Note that Go has the notion of a zero-value. iota starts its life at 0 and increments on every next line. States Created, Initialized, Running and Stopped are defined. To actually make use of State, we should define some instants. We define a type State based on type uint. Printf( "State: %v\n", state) // prints State: 2 Package main import "fmt" type State uint const (

#IOTA GOLANG HOW TO#
The Go language specification and Effective Go: Constants explain how to use iota. Additionally, with the start of every const block, iota gets reset to 0, so it can be used repeatedly. In particular, this is used to define enumerations. With this automatically incrementing counter, it gives you a “short-hand” notation for assigning values to constants. It is a very general construct that is used as an automatically incrementing counter in const declaration blocks. That way you can optimally enjoy iota by inserting new constants at any moment in time / location in the list without the risk of breaking everything. That is, where the constants are referred to by name rather than by value. For example, when implementing parts of a specification and the specification says which values are assigned to which constants, you should explicitly write the constant values.

TL DR Don’t use iota for defining constants where its values are explicitly defined (elsewhere).

When to use Go's iota Thu, ❝Ways of using iota without causing trouble.❞ Contents
