3 ms·
Regarding enum support, you can create type safe enums in Go like this: type Color int const ( Red = iota Blue Green Sausa
by Nitramp 11y ago
Regarding enum support, you can create type safe enums in Go like this:
type Color int
const (
Red = iota
Blue
Green
Sausage
Yellow
)
The `Color` type is not compatible with int, so except for explicit casts and literal assignments, it's type safe.
Though that's quite a different enum style compared to the tagged unions of Rust, his approximation with interfaces is indeed closer to that.
- ibotty 11y agobut that's only a simple c-style enum. What the author is describing is a algebraic data type/sum type: an alternative between types with possibly different types (i.e., a Boolean, two strings, etc.). Read the example in the article closely and you will know what I mean.