Documentation
¶
Overview ¶
Package enum provides a standard enum interface for creating a list of enums. Enums are indexed, iterable, and namespaced.
Flags or flag based enums are still best handled by [Iota]
Basic Enum ¶
Declare an Enum type by embedding Member in a struct.
type Color struct {
enum.Member
}
This enum type can now be used to create an Enum list or namespace.
type Colors struct {
Red Color
Green Color
Blue Color
}
The enums can now be initialized by passing your struct to the Define function.
Colors := enum.Define(Colors{})
Enhanced Enum ¶
Because an enum is just a struct, we can build enhanced enums that carry additional data.
type Vehicle struct {
enum.Member
Tires int
Passengers int
CarbonPerKilometer int
}
These are identical to our basic enum in every other way. Unlike the name and index of enums, the additional data is technically mutable. By convention enhanced enum data should be treated as immutable, though mutation may be desirable in some cases.
Create an Enum list or namespace.
type Vehicles struct {
Car Vehicle
Bus Vehicle
Bicycle Vehicle
}
The additional data can then be populated with their non-zero values when passing the struct declaration to Define.
Vehicle := enum.Define(Vehicles{
Car: Vehicle{
Tires: 4,
Passengers: 5,
CarbonPerKilometer: 400,
},
Bus: Vehicle{
Tires: 6,
Passengers: 50,
CarbonPerKilometer: 800,
},
Bicycle: Vehicle{
Tires: 2,
Passengers: 1,
CarbonPerKilometer: 0,
},
})
Because package enum uses reflection to initialize, it may be advisable to declare your list globally and pass it to Define in the init function
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Enum ¶
type Enum interface {
Index() int
Name() string
String() string
// contains filtered or unexported methods
}
Enum is a package sealed interface to identify the enum type
type Member ¶
type Member struct {
// contains filtered or unexported fields
}
Member is embedded in a struct to mark the struct type as an enum
The zero value of Member is a nil definition signifying the enum is not initialized
func (Member) MarshalText ¶
MarshalText marshals the enum member name