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:
Thanks.
1) You can use stringer to generate the names https://godoc.org/golang.org/x/tools/cmd/stringer
2) Not sure what you mean? Most languages will allow you to drift values, you should always add to the end of the list if you want the number to stay constant, or like in python you could explicitly set each value to a number instead of using iota.
3) Not really, enums just aren't great in golang
Just a suggestion, but something that might help in your case: I find that constants are less likely to be changed/broken later on if it's clear that the values look like bit masks, which you can do in go like so:
const (
Red = 1 << iota
Green
Blue
) // values = 1, 2, 4
And, even though it's not the prettiest of declarations, you can include the mask constants, too
const (
Red, RedMask = 1 << iota, 1<< iota - 1 // Red = 1, RedMask = 0
Green, GreenMask // Green = 2, mask = 1
Blue, BlueMask // 4, 3
RGB, RGBMask // 8, 7
)
This, coupled with a designated type
for these constants might be useful:
type ColourConst int
const (
Red, RMask ColourConst = 1 << iota, 1 << iota-1
// ...
_, All
)
// something like this (untested, might not be correct)
func (c ColourConst) validate() error {
mask := int(c) & (-1 * int(c))
if mask != int(c) {
return errors.New("Colour is not a single bit value")
}
if s := c & All; s != c {
return errors.New("Colour is not in range")
}
}
I know that the days of the week are unlikely to be used as bitmasks, but it makes it less likely for people to break the code. At the very least, it communicates that the order of the constants matter, that's what iota
does IMO.
Solution. There're excellent modules Enumer and Enumelinter