enumgen

command module
v0.0.0-...-4b9bd98 Latest Latest
Warning

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

Go to latest
Published: Jul 25, 2025 License: GPL-3.0 Imports: 14 Imported by: 0

README

enumgen

A utility to generate reverse-lookup tables for enumerated types in Go.

Usage

Usage of enumgen:
        enumgen [flags] -type T [directory]
  -output string
        output file name; default srcdir/<type>_enum.go
  -type string
        comma-separated list of type names; must be set

Example

Given the following declaration and enumgen invocation:

package foo

//go:generate enumgen -type=MyEnum
type MyEnum int

const (
    A MyEnum = iota
    B
    C
)

The generated file will be named myenum_enum.go, and will have the following content:

package foo

var _MyEnumValues = map[int]MyEnum{
        0: A,
        1: B,
        2: C,
}

func GetMyEnum(x int) (MyEnum, bool) {
        v, ok := _MyEnumValues[x]
        return v, ok
}

For a string-typed enum, the generated file will have the following content:

package foo

//go:generate enumgen -type=MyEnum
type MyEnum string

const (
    A MyEnum = "A"
    B MyEnum = "B"
    C MyEnum = "C"
)
package foo

var _MyEnumValues = map[string]MyEnum{
        "A": A,
        "B": B,
        "C": C,
}

func GetMyEnum(x string) (MyEnum, bool) {
        v, ok := _MyEnumValues[x]
        return v, ok
}

Documentation

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

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