enumx

enumx

Define your enum once. enumx generates the type-safe Go API.

Go Reference Tests Go Report Card Coverage Zero dependencies Go version MIT License

GitHub Pkg.go.dev


enumx is a boilerplate-free enum toolkit for Go with zero third-party dependencies. It combines zero-codegen runtime enums with AST-based code generation, and layers validated JSON/SQL, sets, bitmask flags, a state machine, metadata, and localization on top of either.

Why enumx?

  • ๐Ÿšซ No more boilerplate โ€” stop hand-writing type, const, and String().
  • ๐Ÿ”’ Full type-safety โ€” AST codegen produces real types, validation, and autocompletion.
  • ๐Ÿ›ก๏ธ Guarded boundaries โ€” an unregistered value cannot enter or leave via JSON, SQL, or text.
  • ๐ŸŒ Built-in localization โ€” 100+ ISO 639-1 locales with region fallback.
  • ๐Ÿ“ฆ Zero dependencies โ€” stdlib only, enforced in CI.
  • ๐Ÿงช 100% test coverage on both the library and the CLI, all of it under -race.

Installation

go get github.com/bakhod1r/enumx

For the code generator:

go install github.com/bakhod1r/enumx/cmd/enumx@latest

60-second tour

type Status string

const (
    StatusPending Status = "pending"
    StatusActive  Status = "active"
    StatusBlocked Status = "blocked"
)

func init() {
    enumx.Register(StatusPending, StatusActive, StatusBlocked)
}

// Validated at every boundary โ€” JSON, SQL, and text.
type User struct {
    Status enumx.Enum[Status] `json:"status"`
}

// Forgiving parsing for untrusted input.
enumx.ParseLoose[Status]("ACTIVE") // active

// Sets, for "which values are allowed here".
enumx.NewSet(StatusPending, StatusActive).Has(StatusActive) // true

Where to go next

Page What it covers
Code generation enumx generate, the definition file, generated output
Export & lint enumx export to TS/Zod/OpenAPI/SQL, enumx lint for exhaustive switches
Runtime enums New, NewMutable, EnumValue, defaults
Validation & codecs Register, Enum, NullEnum, JSON/SQL, aliases
Sets & flags Set[T], bitmask flags, FlagSet
State machine & metadata transitions, NextStates, Meta, Match
Localization translations, locale fallback, 100+ locales
Changelog release history

License

MIT ยฉ bakhod1r