Using Iota for a List of Constants (Like Enums)
Enums have been used in many programming languages in varying forms for many years. But if you’ve found this post, then you are most likely looking for how to do this in Go (golang). In Go, there’s a handy feature called iota which is a running list of ever-increasing constants. They are a consise way of presenting a list of things in a “don’t repeat yourself” way.
For example if you need to define days of the week using contants, you could:
Which would define Sunday as 0, and Tuesday as 2. They are useful as their value is implied by their order, but this does mean you shouldn’t re-order them after they’ve been written.
When using them, you can use them like any other constant, by their name, but be aware it won’t print the constants name, but it’s value. Here’s an example of us printing each of the values above.