### Install go-enum CLI Tool Source: https://context7.com/abice/go-enum/llms.txt Install go-enum using go get, go install, or Docker. For Go 1.24+, 'go get -tool' is recommended for reproducible builds. ```shell # Go 1.24+ — adds a "tool" line to go.mod for reproducible builds go get -tool github.com/abice/go-enum@latest ``` ```shell # Older Go versions go install github.com/abice/go-enum@latest ``` ```shell # Docker — no local install required docker run -w /app -v $(pwd):/app abice/go-enum:latest ``` -------------------------------- ### Install go-enum Tool Source: https://github.com/abice/go-enum/blob/master/README.md Install go-enum as a project tool using 'go get -tool' for Go 1.24+ or 'go install' for older versions. ```shell go get -tool github.com/abice/go-enum@latest ``` ```shell go install github.com/abice/go-enum@latest ``` -------------------------------- ### Run go-enum Tool Source: https://github.com/abice/go-enum/blob/master/README.md Execute the go-enum tool after installing it with 'go get -tool'. ```shell go tool go-enum [options] ``` -------------------------------- ### Install go-enum as a Tool Source: https://github.com/abice/go-enum/blob/master/README.md Install go-enum as a project tool using 'go get -tool'. This adds it as a dependency in your go.mod file. ```shell go get -tool github.com/abice/go-enum@latest ``` -------------------------------- ### Install go-enum using go install Source: https://github.com/abice/go-enum/blob/master/README.md Install the latest version of go-enum directly from source using 'go install'. This is recommended for older Go versions. ```shell go install github.com/abice/go-enum@latest ``` -------------------------------- ### Go Generate with Tool Dependency (Go 1.24+) Source: https://github.com/abice/go-enum/blob/master/README.md Use 'go generate' with go-enum when using tool dependencies. Ensure the tool is installed first. ```go //go:generate go tool go-enum --marshal ``` -------------------------------- ### Example Enum Definition with Values and Comments Source: https://github.com/abice/go-enum/blob/master/README.md Define an enum with various value assignments, including explicit numeric values and inline comments. This example demonstrates different ways to structure enum definitions. ```go // Color is an enumeration of colors that are allowed. /* ENUM( Black, White, Red Green = 33 // Green starts with 33 */ // Blue // grey= // yellow // blue-green // red-orange // yellow_green // red-orange-blue // ) type Color int32 ``` -------------------------------- ### Example Enum Declaration for Generation Source: https://github.com/abice/go-enum/blob/master/README.md An example of a type declaration with a comment that go-enum will process to generate enum constants and methods. ```go // ENUM(jpeg, jpg, png, tiff, gif) type ImageType int ``` -------------------------------- ### Flag Integration with flag.Value and pflag.Value Source: https://context7.com/abice/go-enum/llms.txt Generate Set, Get, and Type methods to satisfy stdlib flag.Value and github.com/spf13/pflag Value interfaces. Use --nocase for case-insensitive flag parsing. ```go // go tool go-enum -f make.go --flag --nocase // Make x ENUM(Toyota,Chevy,Ford,Tesla) type Make int32 var selectedMake Make // stdlib flag flag.Var(&selectedMake, "make", "car make") flag.Parse() // go run . --make=Tesla → selectedMake == MakeTesla // pflag / cobra cmd.Flags().Var(&selectedMake, "make", "car make") ``` -------------------------------- ### Custom Value Offsets and Skipped Values Source: https://context7.com/abice/go-enum/llms.txt Assign explicit numeric starting points with '=N' and skip iota positions with '_' for custom enum value assignments. ```go // ENUM(Black, White, Red, Green=33, Blue) type Color int // ColorBlack=0, ColorWhite=1, ColorRed=2, ColorGreen=33, ColorBlue=34 // Skip odd positions to leave gaps: // Make x ENUM(Toyota,_,Chevy,_,Ford) type Make int32 // MakeToyota=0, _=1 (skipped), MakeChevy=2, _=3 (skipped), MakeFord=4 // Start at a specific value: // ENUM(start=20, middle, end) type NoZeros int32 // NoZerosStart=20, NoZerosMiddle=21, NoZerosEnd=22 ``` -------------------------------- ### Download go-enum Binary from Releases Source: https://github.com/abice/go-enum/blob/master/README.md Download a pre-built binary for your platform from the GitHub releases page. Make the downloaded file executable. ```shell # Download the latest release for your platform curl -fsSL "https://github.com/abice/go-enum/releases/download/v0.9.0/go-enum_$(uname -s)_$(uname -m)" -o go-enum chmod +x go-enum # Or use a specific version by replacing v0.9.0 with your desired version # Example: curl -fsSL "https://github.com/abice/go-enum/releases/download/v0.8.0/go-enum_$(uname -s)_$(uname -m)" -o go-enum ``` -------------------------------- ### Run go-enum Docker Image (Specific Version) Source: https://github.com/abice/go-enum/blob/master/README.md Execute a specific version of the go-enum Docker image. Mount your current directory to /app for file access. ```shell docker run -w /app -v $(pwd):/app abice/go-enum:v0.9.0 ``` -------------------------------- ### Build Tags (--buildtag) Source: https://context7.com/abice/go-enum/llms.txt Attach one or more Go build tags to the generated file using the --buildtag flag. ```shell go tool go-enum -f color.go --buildtag example --buildtag integration ``` ```go //go:build example && integration // +build example integration ``` -------------------------------- ### Run go-enum via Docker Source: https://github.com/abice/go-enum/blob/master/README.md Execute the go-enum command using a Docker container. Mount your current directory to /app for file access. ```shell docker run -w /app -v $(pwd):/app abice/go-enum:latest ``` -------------------------------- ### Makefile for Standard Enums (Older Go) Source: https://github.com/abice/go-enum/blob/master/README.md Makefile snippet for generating standard enums using the go-enum binary. Assumes the binary is available. ```makefile # The generator statement for go enum files using go-enum binary %_enum.go: %.go $(GOENUM) Makefile $(GOENUM) -f $*.go $(GO_ENUM_FLAGS) ``` -------------------------------- ### Makefile Integration Source: https://context7.com/abice/go-enum/llms.txt Use pattern rules in Makefiles to define generated files as targets, ensuring they are regenerated only when the source changes. ```makefile STANDARD_ENUMS = ./example/animal_enum.go ./example/color_enum.go NULLABLE_ENUMS = ./example/sql_enum.go $(STANDARD_ENUMS): GO_ENUM_FLAGS=--nocase --marshal --names --ptr $(NULLABLE_ENUMS): GO_ENUM_FLAGS=--nocase --marshal --names --sqlnullint --ptr enums: $(STANDARD_ENUMS) $(NULLABLE_ENUMS) %_enum.go: %.go Makefile go tool go-enum -f $*.go $(GO_ENUM_FLAGS) ``` -------------------------------- ### Custom Templates (--template / -t) Source: https://context7.com/abice/go-enum/llms.txt Supply Go text/template files using the --template or -t flags to generate additional code. Templates receive enum metadata and support glob patterns. ```go // user_template.tmpl func Parse{{.enum.Name}}Example() bool { return true } func Parse{{.enum.Name}}Description() string { return `{{.enum.Comment}}` } ``` ```shell go tool go-enum -f user_template.go -t user_template.tmpl -t "*user_glob*.tmpl" ``` -------------------------------- ### Configure Custom JSON Package Source: https://github.com/abice/go-enum/blob/master/README.md Specify a custom JSON package for marshaling/unmarshaling enums. Use the --jsonpkg flag with the desired package path. ```shell go-enum --marshal --jsonpkg="github.com/goccy/go-json" -f your_file.go ``` -------------------------------- ### Generate Enum Code Source: https://github.com/abice/go-enum/blob/master/README.md Run the go-enum tool with the -f flag pointing to your Go file to generate enum methods. ```shell # Go 1.24+ (recommended) go tool go-enum -f your_file.go # Or for older Go versions go-enum -f your_file.go ``` -------------------------------- ### SQL Integration with Scanner and driver.Valuer Source: https://context7.com/abice/go-enum/llms.txt Generate Scan and Value methods to satisfy database/sql's Scanner and driver.Valuer interfaces. Use --sqlnullint and --sqlnullstr for nullable column wrappers. ```go // go tool go-enum -f sql.go --sql --sqlnullint --sqlnullstr --marshal // ENUM(pending, inWork, completed, rejected) type ProjectStatus int // Direct SQL scan/value var status ProjectStatus row.Scan(&status) // reads int64 or string from DB status.Value() // returns driver.Value (string representation) // Nullable int column var ns NullProjectStatus row.Scan(&ns) if ns.Valid { fmt.Println(ns.ProjectStatus) // e.g. ProjectStatusCompleted } ns2 := NewNullProjectStatus("inWork") ns2.Valid // true // Nullable string column var nss NullProjectStatusStr row.Scan(&nss) nss.Value() // returns nil or the string value ``` -------------------------------- ### Go Generate without Tool Dependency Source: https://github.com/abice/go-enum/blob/master/README.md Use 'go generate' with go-enum when not using tool dependencies. Assumes go-enum binary is in your PATH. ```go //go:generate go-enum --marshal ``` -------------------------------- ### Makefile for Standard Enums (Go 1.24+) Source: https://github.com/abice/go-enum/blob/master/README.md Makefile snippet for generating standard enums using 'go tool go-enum'. Defines flags for common options. ```makefile STANDARD_ENUMS = ./example/animal_enum.go \ ./example/color_enum.go NULLABLE_ENUMS = ./example/sql_enum.go $(STANDARD_ENUMS): GO_ENUM_FLAGS=--nocase --marshal --names --ptr $(NULLABLE_ENUMS): GO_ENUM_FLAGS=--nocase --marshal --names --sqlnullint --ptr enums: $(STANDARD_ENUMS) $(NULLABLE_ENUMS) # The generator statement for go enum files using go tool (Go 1.24+) %_enum.go: %.go Makefile go tool go-enum -f $*.go $(GO_ENUM_FLAGS) ``` -------------------------------- ### Pointer Helper (--ptr) Usage Source: https://context7.com/abice/go-enum/llms.txt The --ptr flag generates a .Ptr() method returning *Xxx, useful when a pointer to a constant value is required, such as for optional struct fields or proto3 wrappers. ```go // go tool go-enum -f color.go --ptr type Record struct { Color *Color } r := Record{Color: ColorRed.Ptr()} fmt.Println(*r.Color == ColorRed) // true ``` -------------------------------- ### Use Generated Enum Methods Source: https://github.com/abice/go-enum/blob/master/README.md After generation, you can use the enum constants and call the generated String() and Parse() methods. ```go color := ColorRed fmt.Println(color.String()) // prints "red" parsed, err := ParseColor("green") if err == nil { fmt.Println(parsed) // prints "green" } ``` -------------------------------- ### `go:generate` Integration Source: https://context7.com/abice/go-enum/llms.txt Embed generation commands directly in source files using `//go:generate` directives to keep enums in sync with `go generate ./...`. ```go // color.go //go:generate go tool go-enum --marshal --lower --ptr --mustparse -b example -f color.go package mypackage // Color is an enumeration of colors that are allowed. /* ENUM( Black, White, Red Green = 33 // Green starts with 33 Blue grey= yellow )*/ type Color int32 ``` ```shell # Regenerate all enums in the project: go generate ./... ``` -------------------------------- ### JSON Marshaling with MarshalText and UnmarshalText Source: https://context7.com/abice/go-enum/llms.txt Generate MarshalText and UnmarshalText methods for automatic JSON serialization using human-readable enum names. This requires passing the --marshal flag during generation. ```go // go tool go-enum -f color.go --marshal type Payload struct { Color Color `json:"color"` } data, _ := json.Marshal(Payload{Color: ColorGreen}) fmt.Println(string(data)) // {"color":"Green"} var p Payload _ = json.Unmarshal([]byte(`{"color":"Red"}`), &p) fmt.Println(p.Color == ColorRed) // true // Custom JSON package: // go tool go-enum -f color.go --marshal --jsonpkg="github.com/goccy/go-json" ``` -------------------------------- ### Disable iota (--no-iota) Source: https://context7.com/abice/go-enum/llms.txt Use the --no-iota flag when explicit integer assignments are desired instead of iota-based offsets. ```go // go tool go-enum -f state.go --no-iota // ENUM(off=0, on=1, standby=2) type PowerState int ``` -------------------------------- ### Basic Integer Enum Declaration Source: https://context7.com/abice/go-enum/llms.txt Declare an integer-based enum by annotating a type with an ENUM comment. Use 'go generate' to create the companion enum file. ```go // myenum.go package mypackage //go:generate go tool go-enum -f myenum.go --marshal // ImageType is the set of supported image formats. // ENUM(jpeg, jpg, png, tiff, gif) type ImageType int ``` ```shell go tool go-enum -f myenum.go ``` ```go // Code generated by go-enum DO NOT EDIT. package mypackage import "fmt" const ( ImageTypeJpeg ImageType = iota ImageTypeJpg ImageTypePng ImageTypeTiff ImageTypeGif ) func (x ImageType) String() string { /* ... */ } func ParseImageType(name string) (ImageType, error) { /* ... */ } func (x ImageType) MarshalText() ([]byte, error) { /* ... */ } func (x *ImageType) UnmarshalText(text []byte) error { /* ... */ } func (x ImageType) IsValid() bool { /* ... */ } ``` -------------------------------- ### Custom Constant Prefixes with --prefix Source: https://context7.com/abice/go-enum/llms.txt Override the default TypeName prefix on generated constants using the --prefix flag. Use --noprefix with --prefix to replace the type name entirely. ```go // custom_prefix.go //go:generate go tool go-enum -f custom_prefix.go --prefix=AcmeInc // Products of AcmeInc ENUM(Anvil, Dynamite, Glue) type Product int32 ``` ```go // Generated constants use the custom prefix instead of "Product": const ( AcmeIncAnvil Product = iota AcmeIncDynamite AcmeIncGlue ) p := AcmeIncAnvil fmt.Println(p.String()) // "Anvil" ``` -------------------------------- ### Generated Enum Code Structure Source: https://github.com/abice/go-enum/blob/master/README.md Illustrates the structure of code automatically generated by go-enum, including constants, Stringer, and MarshalText/UnmarshalText methods. ```go const ( // ImageTypeJpeg is a ImageType of type Jpeg. ImageTypeJpeg ImageType = iota // ImageTypeJpg is a ImageType of type Jpg. ImageTypeJpg // ImageTypePng is a ImageType of type Png. ImageTypePng // ImageTypeTiff is a ImageType of type Tiff. ImageTypeTiff // ImageTypeGif is a ImageType of type Gif. ImageTypeGif ) // String implements the Stringer interface. func (x ImageType) String() string // ParseImageType attempts to convert a string to a ImageType. func ParseImageType(name string) (ImageType, error) // MarshalText implements the text marshaller method. func (x ImageType) MarshalText() ([]byte, error) // UnmarshalText implements the text unmarshaller method. func (x *ImageType) UnmarshalText(text []byte) error ``` -------------------------------- ### List All Enum Names and Values Source: https://context7.com/abice/go-enum/llms.txt Generate Names() and Values() functions to retrieve all valid string representations and typed enum values. This also enriches parse errors with the list of valid values. ```go // go tool go-enum -f make.go --names --values names := MakeNames() // ["Toyota", "Chevy", "Ford", "Tesla", "Hyundai", ...] vals := MakeValues() // [MakeToyota, MakeChevy, MakeFord, ...] _, err := ParseMake("Unknown") // err: "Unknown is not a valid Make, try [Toyota, Chevy, Ford, ...]" ``` -------------------------------- ### String Enum with SQL Integer Support Source: https://github.com/abice/go-enum/blob/master/README.md Use string enums while also providing integer values for SQL. This is useful for API layers and SQL layers needing consistent types but different representations. ```go // swagger:enum StrState // ENUM(pending, running, completed, failed) type StrState string ``` -------------------------------- ### Generated Constants with Comments Source: https://github.com/abice/go-enum/blob/master/README.md This shows how comments from the ENUM definition are translated into Go constants. Comments are placed directly above their corresponding constants. ```go ... const ( // CommentedValue1 is a Commented of type Value1 // Commented value 1 CommentedValue1 Commented = iota // CommentedValue2 is a Commented of type Value2 CommentedValue2 // CommentedValue3 is a Commented of type Value3 // Commented value 3 CommentedValue3 ) ... ``` -------------------------------- ### Declare String Enum Constants Source: https://github.com/abice/go-enum/blob/master/README.md Define the constants for a string-based enum. Each constant is assigned a string value corresponding to its name. ```go const ( // StrStatePending is a StrState of type pending. StrStatePending StrState = "pending" // StrStateRunning is a StrState of type running. StrStateRunning StrState = "running" // StrStateCompleted is a StrState of type completed. StrStateCompleted StrState = "completed" // StrStateFailed is a StrState of type failed. StrStateFailed StrState = "failed" ) ``` -------------------------------- ### MustParseXxx Panicking Parse Source: https://context7.com/abice/go-enum/llms.txt The --mustparse flag generates a MustParseXxx variant that panics on invalid input, suitable for init() or test code. ```go // go tool go-enum -f color.go --mustparse color := MustParseColor("Blue") // returns ColorBlue color = MustParseColor("invisible") // panics: "invisible is not a valid Color" ``` -------------------------------- ### Generated Go Enum Code with Stringer and Parser Source: https://github.com/abice/go-enum/blob/master/README.md This is the complete generated Go code for the `Color` enum. It includes constants, string mapping, parsing functions, and text marshalling/unmarshalling methods. ```go // Code generated by go-enum DO NOT EDIT. // Version: example // Revision: example // Build Date: example // Built By: example package example import ( "fmt" "strings" ) const ( // ColorBlack is a Color of type Black. ColorBlack Color = iota // ColorWhite is a Color of type White. ColorWhite // ColorRed is a Color of type Red. ColorRed // ColorGreen is a Color of type Green. // Green starts with 33 ColorGreen Color = iota + 30 // ColorBlue is a Color of type Blue. ColorBlue // ColorGrey is a Color of type Grey. ColorGrey // ColorYellow is a Color of type Yellow. ColorYellow // ColorBlueGreen is a Color of type Blue-Green. ColorBlueGreen // ColorRedOrange is a Color of type Red-Orange. ColorRedOrange // ColorYellowGreen is a Color of type Yellow_green. ColorYellowGreen // ColorRedOrangeBlue is a Color of type Red-Orange-Blue. ColorRedOrangeBlue ) const _ColorName = "BlackWhiteRedGreenBluegreyyellowblue-greenred-orangeyellow_greenred-orange-blue" var _ColorMap = map[Color]string{ ColorBlack: _ColorName[0:5], ColorWhite: _ColorName[5:10], ColorRed: _ColorName[10:13], ColorGreen: _ColorName[13:18], ColorBlue: _ColorName[18:22], ColorGrey: _ColorName[22:26], ColorYellow: _ColorName[26:32], ColorBlueGreen: _ColorName[32:42], ColorRedOrange: _ColorName[42:52], ColorYellowGreen: _ColorName[52:64], ColorRedOrangeBlue: _ColorName[64:79], } // String implements the Stringer interface. func (x Color) String() string { if str, ok := _ColorMap[x]; ok { return str } return fmt.Sprintf("Color(%d)", x) } var _ColorValue = map[string]Color{ _ColorName[0:5]: ColorBlack, strings.ToLower(_ColorName[0:5]): ColorBlack, _ColorName[5:10]: ColorWhite, strings.ToLower(_ColorName[5:10]): ColorWhite, _ColorName[10:13]: ColorRed, strings.ToLower(_ColorName[10:13]): ColorRed, _ColorName[13:18]: ColorGreen, strings.ToLower(_ColorName[13:18]): ColorGreen, _ColorName[18:22]: ColorBlue, strings.ToLower(_ColorName[18:22]): ColorBlue, _ColorName[22:26]: ColorGrey, strings.ToLower(_ColorName[22:26]): ColorGrey, _ColorName[26:32]: ColorYellow, strings.ToLower(_ColorName[26:32]): ColorYellow, _ColorName[32:42]: ColorBlueGreen, strings.ToLower(_ColorName[32:42]): ColorBlueGreen, _ColorName[42:52]: ColorRedOrange, strings.ToLower(_ColorName[42:52]): ColorRedOrange, _ColorName[52:64]: ColorYellowGreen, strings.ToLower(_ColorName[52:64]): ColorYellowGreen, _ColorName[64:79]: ColorRedOrangeBlue, strings.ToLower(_ColorName[64:79]): ColorRedOrangeBlue, } // ParseColor attempts to convert a string to a Color func ParseColor(name string) (Color, error) { if x, ok := _ColorValue[name]; ok { return x, nil } return Color(0), fmt.Errorf("%s is not a valid Color", name) } func (x Color) Ptr() *Color { return &x } // MarshalText implements the text marshaller method func (x Color) MarshalText() ([]byte, error) { return []byte(x.String()), nil } // UnmarshalText implements the text unmarshaller method func (x *Color) UnmarshalText(text []byte) error { name := string(text) tmp, err := ParseColor(name) if err != nil { return err } *x = tmp return nil } ``` -------------------------------- ### Handle Non-Alphanumeric Value Aliases Source: https://context7.com/abice/go-enum/llms.txt Map characters invalid in Go identifiers (e.g., '+', '#', '-') to identifier-safe names using the --alias flag with 'key:value' pairs. ```go // animal.go //go:generate go tool go-enum -f animal.go -a "+:Plus,#:Sharp" // Animal x ENUM(Cat, Dog, Fish, Fish++, Fish#) type Animal int32 ``` ```go // Generated: const ( AnimalCat Animal = iota AnimalDog AnimalFish AnimalFishPlusPlus // "+" → "Plus" AnimalFishSharp // "#" → "Sharp" ) ``` -------------------------------- ### Stringer Interface Implementation Source: https://context7.com/abice/go-enum/llms.txt Generated enums implement fmt.Stringer. Non-listed values return a fallback string representation. ```go // ENUM(Black, White, Red, Green=33, Blue) type Color int color := ColorGreen fmt.Println(color.String()) // "Green" unknown := Color(99) fmt.Println(unknown.String()) // "Color(99)" ``` -------------------------------- ### Set Custom Output Suffix Source: https://github.com/abice/go-enum/blob/master/README.md Change the default suffix for generated enum files. Use the --output-suffix flag followed by the desired suffix. ```shell go-enum --output-suffix="_generated" -f your_file.go # Creates your_file_generated.go ``` -------------------------------- ### Declare an Enum Type Source: https://github.com/abice/go-enum/blob/master/README.md Define an enum by declaring a type and using a comment with the ENUM() directive to list its values. ```go package main // ENUM(red, green, blue) type Color int ``` -------------------------------- ### Custom Output Suffix (--output-suffix) Source: https://context7.com/abice/go-enum/llms.txt Change the default `_enum` suffix of generated files using the --output-suffix flag. This can be applied to regular files or test files. ```shell # Creates myfile_generated.go instead of myfile_enum.go go tool go-enum -f myfile.go --output-suffix="_generated" # For test files (_test.go → _enum_test.go by default): go tool go-enum -f myfile.go --output-suffix=".enum.gen" # Creates myfile.enum.gen.go ``` -------------------------------- ### Define Enum with Comments in Go Source: https://github.com/abice/go-enum/blob/master/README.md Use `//` comments at the end of a line within the ENUM definition to associate comments with generated constants. These comments will appear above the generated constants. ```go // Commented is an enumeration of commented values /* ENUM( value1 // Commented value 1 value2 value3 // Commented value 3 ) */ type Commented int ``` -------------------------------- ### Case Control Flags (--lower, --nocase, --forcelower, --forceupper) Source: https://context7.com/abice/go-enum/llms.txt These flags control how case is handled in generated enum names and parse maps. --lower adds lowercase variants, --nocase enables full case-insensitive parsing, --forcelower lowercases CamelCase comment values in generated names, and --forceupper uppercases them. ```go // --forcelower example // ENUM(DataSwap, BootNode) type ForceLowerType int // Generated: ForceLowerTypeDataswap, ForceLowerTypeBootnode ``` ```go // --forceupper example // ENUM(DataSwap, BootNode) type ForceUpperType int // Generated: ForceUpperTypeDataswap → DATASWAP form ``` -------------------------------- ### ParseXxx String-to-Enum Conversion Source: https://context7.com/abice/go-enum/llms.txt A ParseXxx function is generated for string-to-enum conversion, returning a typed error for invalid inputs. ```go c, err := ParseColor("Red") if err != nil { // err wraps ErrInvalidColor if errors.Is(err, ErrInvalidColor) { log.Fatal("not a valid color") } } fmt.Println(c == ColorRed) // true _, err = ParseColor("ultraviolet") // err: "ultraviolet is not a valid Color" ``` -------------------------------- ### Suppress Parse Method (--noparse) Source: https://context7.com/abice/go-enum/llms.txt Use the --noparse flag to omit the public ParseXxx function, creating a simpler read-only enum. The String(), IsValid(), and constants are still produced. ```go // go tool go-enum -f noparse.go --noparse // ENUM(A, B, C, D, E) type UnparsedValues uint8 // No ParseUnparsedValues() function is generated. // String(), IsValid(), and constants are still produced. fmt.Println(UnparsedValuesA.String()) // "A" ``` -------------------------------- ### Define String Typed Enum Source: https://github.com/abice/go-enum/blob/master/README.md Declare a string-based enum type. This is the basic syntax for string enums. ```go // ENUM(pending, running, completed, failed) type StrState string ``` -------------------------------- ### Inline Comments in ENUM Declaration Source: https://context7.com/abice/go-enum/llms.txt Comments placed after a value on the same line are carried through into the generated constant documentation. ```go /* ENUM( value1 // Commented value 1 value2 value3 // Commented value 3 ) */ type Commented int ``` ```go const ( // CommentedValue1 is a Commented of type Value1. // Commented value 1 CommentedValue1 Commented = iota // CommentedValue2 is a Commented of type Value2. CommentedValue2 // CommentedValue3 is a Commented of type Value3. // Commented value 3 CommentedValue3 Commented3 ) ``` -------------------------------- ### String-Based Enum Declaration Source: https://context7.com/abice/go-enum/llms.txt Declare a string-based enum using a string underlying type. Options like --nocase and --mustparse can be applied. ```go // state.go package mypackage //go:generate go tool go-enum -f state.go --marshal --nocase --mustparse // ENUM(pending, running, completed, failed=error) type StrState string ``` ```go // Generated constants: const ( StrStatePending StrState = "pending" StrStateRunning StrState = "running" StrStateCompleted StrState = "completed" StrStateFailed StrState = "error" // custom string value ) // Usage s := StrStatePending fmt.Println(s.String()) // "pending" fmt.Println(s.IsValid()) // true val, err := ParseStrState("running") // val == StrStateRunning, err == nil val, err = ParseStrState("RUNNING") // case-insensitive with --nocase // val == StrStateRunning, err == nil val = MustParseStrState("completed") // panics on invalid input // val == StrStateCompleted ``` -------------------------------- ### Check Enum Value Validity with IsValid() Source: https://context7.com/abice/go-enum/llms.txt Use the generated IsValid() method to check if an enum value is valid. This is useful for guarding incoming values before processing. ```go fmt.Println(ColorRed.IsValid()) // true fmt.Println(Color(999).IsValid()) // false // Guard incoming values: func SetBackground(c Color) error { if !c.IsValid() { return fmt.Errorf("invalid color: %v", c) } // ... return nil } ``` -------------------------------- ### Disable iota in Enum Generation Source: https://github.com/abice/go-enum/blob/master/README.md Prevent the use of iota in generated enums by using the --no-iota flag. This is useful when you need explicit control over values. ```shell go-enum --no-iota -f your_file.go ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.