Bad parts of Go
Enums (sum types, tagged unions or wtv)
- go enums are error-prone, verbose and dont actually enforce type
- more of just an alias
type Color string
const (
Red Color = "Red"
Green Color = "Green"
Blue Color = "Blue"
)
// for some reason, this still works
var carColor Color = "clearly not a color"
This is because the compiler does not care since Color
type is just an alias for string
hence any string
is a valid Color
There is no way to enforce this at compile time, so we are forced to write runtime checks
switch color {
case Red:
// do happy thing
case Green:
// do other happy thing
case Blue:
// do other other happy thing
default:
return errors.New("wHY dIDn'T yOU uSe a vALID cOLoR???")
}