Basic Examples

Learn how to use the core functionality of goenums with simple examples.

Simple Enum Definition

Here’s a simple enum definition for task statuses:

package validation

type status int

//go:generate goenums status.go
const (
    unknown   status = iota // invalid Unknown
    pending                 // Pending
    approved                // Approved
    rejected                // Rejected
    completed               // Completed
)

This enum has a standard alias for each enum value defined in the comment.

Generate the enum implementations with the goenums tool:

$ go generate ./...

Custom String Representations

As with the above example, we can add a custom string representation to each enum value:

Standard Name Comment

type ticketStatus int

//go:generate goenums status.go
const (
    unknown   ticketStatus = iota // invalid Unknown
    pending                       // Pending
    approved                      // Approved
    rejected                      // Rejected
    completed                     // Completed
)

Name Comment with spaces

When using alias names that contain spaces, the double quotes are required:

package validation

type ticketStatus int

//go:generate goenums status.go
const (
    unknown   ticketStatus = iota // invalid "Not Found"
    pending                       // "In Progress"
    approved                      // "Fully Approved"
    rejected                      // "Has Been Rejected"
    completed                     // "Successfully Completed"
)

Case Insensitive String Parsing

Use the -i flag to enable case insensitive string parsing:

//go:generate goenums -i status.go

Generated code will parse case insensitive strings. All of the below will validate and produce the pending enum:

status, err := validation.ParseTicketStatus("In Progress")
if err != nil {
    fmt.Println("error:", err)
}
status, err := validation.ParseTicketStatus("in progress")
if err != nil {
    fmt.Println("error:", err)
}
status, err := validation.ParseTicketStatus("IN PROGRESS")
if err != nil {
    fmt.Println("error:", err)
}

JSON, Text, Binary, and Database Storage

The generated enum type implements the:

  • json.Unmarshal and json.Marshal interfaces
  • sql.Scanner and sql.Valuer interfaces
  • encoding.BinaryMarshaler and encoding.BinaryUnmarshaler interfaces
  • encoding.TextMarshaler and encoding.TextUnmarshaler interfaces

These interfaces allow you to use the enum type in JSON, text, binary, and database storage seamlessly.

Basic Usage After Generation

Use the generated code in your Go project:


ticketStatus := validation.TicketStatuses.PENDING

// Convert to string
fmt.Println(ticketStatus.String()) // "PENDING"

// Parse from various sources
input := "SKIPPED"
parsed, _ := validation.ParseTicketStatus(input)

// Validate enum values
if !parsed.IsValid() {
    fmt.Println("Invalid status")
}

// JSON marshaling/unmarshaling
type Task struct {
    ID     int              `json:"id"`
    Status validation.TicketStatus `json:"status"`
}

// Iterate through all values
for status := range validation.Statuses.All() {
    fmt.Printf("Status: %s\n", status)
}

Back to Examples