Go by Example: Enums
Go by Example : Enums
Enumerated types (enums) are a special case of sum types . An enum is a type that has a fixed number of possible values, each with a distinct name. Go doesn’t have an enum type as a distinct language feature, but enums are simple to implement using existing language idioms. | |
![]() | |
| |
Our enum type
| |
The possible values for
| |
By implementing the
fmt.Stringer
interface, values of
This can get cumbersome if there are many possible values. In such
cases the
stringer tool
can be used in conjunction with
| |
| |
If we have a value of type
| |
| |
transition emulates a state transition for a server; it takes the existing state and returns a new state. | |
Suppose we check some predicates here to determine the next state… | |
|
Next example: Struct Embedding .
