3 ms·
Enums exist specifically to be compared with one of their possible values, how can you have a zero value problem?
by riquito 7y ago
Enums exist specifically to be compared with one of their possible values, how can you have a zero value problem?
- gen220 7y agoIf the caller of your interface does not specify a value for your enum, they have implicitly specified the zero value. Whether that’s desirable behavior or not is up to you. For most clients, this behavior can be surprising if the zero value is a meaningful one (i.e. one that implies an intentional choice). IME it’s useful to explicitly define the zero value’s meaning as “UNSPECIFIED”, to simplify the problem of trying to guess if the client intended to pass a zero value.
- reificator 7y ago> Enums exist specifically to be compared with one of their possible values, how can you have a zero value problem? Because there's not actually support for enums in go. There's support for constant values, and automatically assigning sequential values to them. That happens to be useful for solving the same kinds of problems that enums solve, but they're not equivalent.