Go R1 Day 85
tech development 100DaysOfCode golang microblog
progress
🎉Finished Ultimate Syntax course.
Worked on Enumerators concept using iota.
I still find this very confusing in general.
Here’s the gist I created.
[help] enumeration with go
# Enumeration With Go
Usage of named aliases for types in an effort to emulate enumeration behavior from other languages can cause issues.
Using a named type for an alias gives the illusion of control, but since a literal value can be placed as it matches the type, it doesn't really do anything but provide an additional layer of abstraction.
Example:
- [Overriding the Values](https://play.golang.org/p/usaqkvrEi4m)
- [Iota Not Allowing Override With String](https://play.golang.org/p/WHD_zYsruq6)
A possible example of using Iota, while still providing a string value output is shown here: [playground](https://play.golang.org/p/DfP9EQkaAK4).
This protects the code to do use an iota constant, while still ensuring a simple string value represents it.
NOTE: I believe this is what `stringer` package trys to simplify, in you can generate the basic structure illustrated above with less effort by having generation of these sets of constants done automatically.
## Alternatives
- Stringer to generate iota based enums dynamically.
- Don't use enum.
## Reference
- [Playground - How Iota Works](https://play.golang.org/p/SLAYYNFIdUA)
- [Start Enumeration at One](https://github.com/uber-go/guide/blob/master/style.md#start-enums-at-one)
- [gotraining/README.md at master · ardanlabs/gotraining · GitHub](https://github.com/ardanlabs/gotraining/blob/master/topics/go/language/constants/README.md)