I'm starting to learn Go after other languages. Go has a very elegant way of creating constants with numeric values like:
const (
_ = iota // 0 and is skipped
Sunday // 1
Monday // 2
...
)
This is very easy to write, but is it really easy to maintain? For example, if you suddenly insert new value to between present, all subsequent will change their values. And it will be hard to find, only scrupulous diff reading can reveal it. Or errors on other parts. How can I extract these values with names and use in other parts of a program, or in database? For example for PostgreSQL I can define:
CREATE TYPE color AS ENUM ('', 'Sunday', 'Monday');
Just to illustrate an idea. For example, Python has Enum type:
from enum import Enum
class Color(Enum):
RED = 1
GREEN = 2
BLUE = 3
Then you may use it like Color.RED
. Next I can take all values:
list(Color)
[<Color.RED: 1>, <Color.BLUE: 2>, <Color.GREEN: 3>]
This allows me to "introspect" to module and create easily-readable enums in databases. For example for PostgreSQL I can define:
CREATE TYPE color AS ENUM ('RED', 'GREEN', 'BLUE');
How can I:
- Reflect golang constants names?
- Make error-proof constants which cannot drift their values? Only fix them manually?
- May be there's an idiomatic way to do it better?
Thanks.