Help us shape the future of Go! Take the 2026 Go Developer Survey

enum

package
v1.0.0-rc.12 Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2026 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package enum holds the storage and JSON codec shared by small state enums: a uint8 whose values run 1..n, each spelled by one word. A type keeps its own constants, names table and transition rules, and forwards its String, Parse, Value, Scan and JSON methods here in one line each.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Set

type Set[T ~uint8] struct {
	// Names spells each value: Names[i] is the word for T(i+1).
	Names []string
	// Invalid is what String returns for a value outside the set.
	Invalid string
	// Err is wrapped by every parse, range and scan error, so callers can
	// test for it with errors.Is.
	Err error
	// ScanLabel names a non-text database value in a Scan error, such as
	// "value" or "database value".
	ScanLabel string
}

Set is the codec of one enum type. The zero value of T is never valid.

func (Set[T]) JSON

func (s Set[T]) JSON(v T) ([]byte, error)

JSON writes v as its word; a value outside the set is an error.

func (Set[T]) Parse

func (s Set[T]) Parse(text string) (T, error)

Parse reads a word back into its value.

func (Set[T]) ReadJSON

func (s Set[T]) ReadJSON(dst *T, data []byte) error

ReadJSON reads a JSON string word into dst.

func (Set[T]) Scan

func (s Set[T]) Scan(dst *T, value any) error

Scan reads a stored word into dst. Only text is accepted: NULL and other types are errors, and dst is left unchanged on any error.

func (Set[T]) String

func (s Set[T]) String(v T) string

String is v's word, or Invalid.

func (Set[T]) Valid

func (s Set[T]) Valid(v T) bool

Valid reports whether v is one of the set's values.

func (Set[T]) Value

func (s Set[T]) Value(v T) (driver.Value, error)

Value stores v as its word; a value outside the set is refused.

Jump to

Keyboard shortcuts

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