enumer-str

command module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jan 27, 2026 License: BSD-2-Clause-Views Imports: 15 Imported by: 0

README

Enumer-str GoDoc Go Report Card GitHub Release

Enumer is a tool to generate Go code that adds useful methods to Go enums (constants with a specific type). It started as a fork of Rob Pike’s Stringer tool maintained by Álvaro López Espinosa. This was again forked here as (https://github.com/dmarkham/enumer) picking up where Álvaro left off. And the current version was forked here as (https://github.com/ryanfkeepers/enumer-str) to support string consts.

This is an experimental fork. No support is guaranteed.

$ enumer-str --help
Enumer is a tool to generate Go code that adds useful methods to Go enums (constants with a specific type).
Usage of enumer:
        Enumer [flags] -type T [directory]
        Enumer [flags] -type T files... # Must be a single package
For more information, see:
        http://godoc.org/github.com/ryanfkeepers/enumer-str
Flags:
  -type string
        comma-separated list of type names; must be set
  -comment value
        comments to include in generated code, can repeat. Default: ""
  -output string
        output file name; default srcdir/<type>_string.go

Generated functions and methods

When Enumer is applied to a type, it will generate:

  • The following basic methods/functions:

    • Function <Type>Values(): returns a slice with all the values of the enum
    • Method IsValid(): returns true only if the current value is among the values of the enum. Useful for validations.

For example, if we have an enum type called Pill,

type Pill string

const (
	Placebo       Pill = "placebo"
	Aspirin       Pill = "aspirin"
	Ibuprofen     Pill = "ibuprofen"
	Paracetamol   Pill = "paracetamol"
	Acetaminophen Pill = "acetaminophen"
)

executing enumer -type=Pill -json will generate a new file with four basic methods and two extra for JSON:

func PillValues() []Pill {
	//...
}

func (i Pill) IsValid() bool {
	//...
}

From now on, we can:

// Convert any Pill value to string
var aspirinString string = Aspirin.String()
// (or use it in any place where a Stringer is accepted)
fmt.Println("I need ", Paracetamol) // Will print "I need Paracetamol"

// Get all the values of the string
allPills := PillValues()
fmt.Println(allPills) // Will print [Placebo Aspirin Ibuprofen Paracetamol]

// Check if a value belongs to the Pill enum values
var notAPill Pill = "Erestor"
if notAPill.IsValid() {
	fmt.Println(notAPill, "is not a value of the Pill enum")
}

alsoNotAPill := "Lacrimosa"
if IsAValidPill(alsoNotAPill) {
	fmt.Println(notAPill, "is not a value of the Pill enum")
}

How to use

For a module-aware repo with enumer in the go.mod file, generation can be called by adding the following to a .go source file:

//go:generate go run github.com/ryanfkeepers/enumer-str -type=YOURTYPE

Inspiring projects

Documentation

Overview

enumer-str is a tool to generate Go code for set validation of typed sets of string constants. It started as a fork of the enumer tool

Please visit http://github.com/ryanfkeepers/enumer-str for a comprehensive documentation

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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