enum

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 1, 2026 License: BSD-3-Clause Imports: 4 Imported by: 0

README

Enum

The enum package is a way to build typed registries in Go. Enums are simply the first and most common registry.

Unlike other Go enum packages, this is not a code generation tool. The package works at runtime and requires zero additional dependencies.

Flags or flag based enums are still best handled by Iota

Requirements

  • Go 1.26.0 or later (recommended for latest capabilities)
  • Go 1.18.0 or later (minimum for Go Generics dependency)

Installation and Usage

The import path for the package is github.com/gibriil/enum.

To install it, run:

go get github.com/gibriil/enum@latest

API Documentation

See: https://pkg.go.dev/gibriil/enum

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

func Define

func Define[T any](schema T) T

Define registers the struct enum namespace and uses reflection over the struct fields to initialize each enum member

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) Index

func (e Member) Index() int

Index returns the index of member's position in the enum list

func (Member) IsZero

func (e Member) IsZero() bool

IsZero reports whether the member is its zero value

func (Member) MarshalText

func (e Member) MarshalText() ([]byte, error)

MarshalText marshals the enum member name

func (Member) Name

func (e Member) Name() string

Name returns the enum member name

func (Member) String

func (e Member) String() string

String returns the enum member name

func (Member) Valid

func (e Member) Valid() bool

Valid reports whether or not the enum has been initialized

func (Member) Value

func (e Member) Value() (driver.Value, error)

Value allows the driver to handle the name of the enum member

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL