/enum

A enum library for Go

Primary LanguageGoApache License 2.0Apache-2.0

Go Reference

Enum

An easy-to-use Go library for working with enums.

It simplifies serialization, deserialization, and supports constant enums, all without the need for code generation.

Features

  • No Code Generation: Simplifies usage by eliminating the need for additional tools or build steps.
  • Supports Constant Enums: Enables defining immutable enum values for safer and more predictable behavior.
  • Compatible with Standard Enum Definitions: Easily integrates with Go's conventional enum patterns, including iota.
  • Easy Conversion: Effortlessly convert between numeric and string representations for better usability and flexibility.
  • Serializable: Provides JSON serialization and deserialization with enum.RichEnum, making it easier to work with enums in JSON.

Usage

Define enum using enum.New

The enum.New function allows dynamic initialization of enum values and maps them to a string representation.

type Role int

var (
    RoleUser  = enum.New[Role]("user")  // Dynamically creates and maps "user"
    RoleAdmin = enum.New[Role]("admin") // Dynamically creates and maps "admin"
)

Define enum using enum.Map

The enum.Map function supports defining constant enums and is fully compatible with Go's standard enum patterns.

type Role int

const (
    RoleUser Role = iota
    RoleAdmin
)

var (
    _ = enum.Map(RoleUser, "user")   // Maps RoleUser to "user"
    _ = enum.Map(RoleAdmin, "admin") // Maps RoleAdmin to "admin"
)

With enum.Map, you can associate string values with constants, making them easier to work with in serialization or user-facing output.

Rich Enum

enum.RichEnum is a struct with set of utility methods to simplify working with enums.

It includes various helper functions for operations like serialization, deserialization, string conversion, and validation, making it easier to manage and manipulate enum values across your codebase.

type unsafeRole any
type Role = enum.RichEnum[unsafeRole] // NOTE: It must use type alias instead of type definition.

const (
    RoleUser Role = iota
    RoleAdmin
)

var (
    _ = enum.Map(RoleUser, "user")   // Maps RoleUser to "user"
    _ = enum.Map(RoleAdmin, "admin") // Maps RoleAdmin to "admin"
)

data, err := json.Marshal(RoleUser)         // data = "user"
fmt.Printf("%d\n", RoleAdmin)               // Output: 1
fmt.Printf("%s\n", RoleAdmin)               // Output: admin
fmt.Println(RoleAdmin.IsValid())            // Output: true