
goenum
Go enumeration generation and management toolkit with type safety and flexible naming patterns.
CHINESE README
ไธญๆ่ฏดๆ
Key Features
๐ฏ Smart Enum Generation: Auto generates type-safe enum code with customizable naming patterns
โก Multiple Naming Modes: Supports prefix, suffix, middle, and single naming strategies
๐ Type Safety: Compile-time enum validation with generic constraints
๐ Flexible Types: Works with any comparable type (int, string, custom types)
๐ Validation Functions: Auto-generates Valid() and Check() methods for runtime validation
Installation
go get github.com/yyle88/goenum
Quick Start
1. Define Your Enum Configuration
package main
import (
"github.com/yyle88/goenum/goenumgen"
)
func main() {
// Configure enum generation
config := &goenumgen.Config[string]{
Type: "StatusEnum",
Name: "Status",
BasicValue: "Status",
DelimValue: "-",
NamingMode: goenumgen.NamingMode.Suffix(), // "Status-OK", "Status-Error"
IsGenValid: true,
IsGenCheck: true,
Options: []*goenumgen.EnumOption[string]{
{Name: "OK", OptionValue: "OK"},
{Name: "Error", OptionValue: "Error"},
{Name: "Pending", OptionValue: "Pending"},
},
}
// Generate enum code
goenumgen.Generate(config, "internal/enums/status.go")
}
2. Generated Enum Code
The above configuration generates:
package enums
import "slices"
type StatusEnum string
const Status = StatusEnum("Status")
func (StatusEnum) OK() StatusEnum {
return "Status" + "-" + "OK"
}
func (StatusEnum) Error() StatusEnum {
return "Status" + "-" + "Error"
}
func (StatusEnum) Pending() StatusEnum {
return "Status" + "-" + "Pending"
}
func (StatusEnum) Enums() []StatusEnum {
return []StatusEnum{
Status.OK(),
Status.Error(),
Status.Pending(),
}
}
func (value StatusEnum) Valid() bool {
return slices.Contains(Status.Enums(), value)
}
func (value StatusEnum) Check() bool {
return value == Status || slices.Contains(Status.Enums(), value)
}
3. Use Your Enums
package main
import (
"fmt"
"your-project/internal/enums"
"github.com/yyle88/goenum"
)
func main() {
// Create enum values
status := enums.Status.OK()
fmt.Println(status) // Output: Status-OK
// Validate enum values
if goenum.Valid(status) {
fmt.Println("Valid enum value")
}
// Check with basic value support
if goenum.Check(enums.Status) {
fmt.Println("Basic value is valid")
}
// Get all enum values
allStatuses := enums.Status.Enums()
for _, s := range allStatuses {
fmt.Printf("Status: %s, Valid: %t\n", s, s.Valid())
}
}
Naming Modes
Prefix Mode
Pattern: option + delimiter + basic
NamingMode: goenumgen.NamingMode.Prefix()
// Result: "OK-Status", "Error-Status"
Suffix Mode
Pattern: basic + delimiter + option
NamingMode: goenumgen.NamingMode.Suffix()
// Result: "Status-OK", "Status-Error"
Middle Mode
Pattern: basic + option + delimiter
NamingMode: goenumgen.NamingMode.Middle()
// Result: "StatusOK-", "StatusError-"
Single Mode
Pattern: option
NamingMode: goenumgen.NamingMode.Single()
// Result: "OK", "Error"
Advanced Examples
HTTP Status Codes
config := &goenumgen.Config[int]{
Type: "HTTPStatusEnum",
Name: "HTTPStatus",
BasicValue: 0,
DelimValue: 0, // Not used for integers
NamingMode: goenumgen.NamingMode.Single(),
IsGenValid: true,
IsGenCheck: true,
Options: []*goenumgen.EnumOption[int]{
{Name: "OK", OptionValue: 200},
{Name: "NotFound", OptionValue: 404},
{Name: "InternalError", OptionValue: 500},
},
}
Database Connection States
config := &goenumgen.Config[string]{
Type: "ConnStateEnum",
Name: "ConnState",
BasicValue: "conn",
DelimValue: ".",
NamingMode: goenumgen.NamingMode.Prefix(),
IsGenBasic: true,
IsGenValid: true,
Options: []*goenumgen.EnumOption[string]{
{Name: "Connected", OptionValue: "active"},
{Name: "Disconnected", OptionValue: "inactive"},
{Name: "Connecting", OptionValue: "pending"},
},
}
// Generates: "active.conn", "inactive.conn", "pending.conn"
Configuration Options
| Field |
Type |
Description |
Type |
string |
Generated enum type name |
Name |
string |
Base constant name |
BasicValue |
T |
Basic value for the enum |
DelimValue |
T |
Delimiter for compound names |
Options |
[]*EnumOption[T] |
Enum option definitions |
NamingMode |
NamingModeEnum |
Naming pattern strategy |
IsGenBasic |
bool |
Generate Basic() method |
IsGenValid |
bool |
Generate Valid() method |
IsGenCheck |
bool |
Generate Check() method |
Validation Functions
goenum.Valid()
Checks if a value exists in the enum set:
if goenum.Valid(status) {
// Value is one of the defined enum options
}
goenum.Check()
Validates with basic value fallback:
if goenum.Check(status) {
// Value is either basic value or valid enum option
}
Project Structure
goenum/
โโโ goenum.go # Main validation functions
โโโ goenumgen/ # Code generation package
โ โโโ generate.go # Generation engine
โ โโโ naming_mode.go # Naming pattern definitions
โโโ internal/
โ โโโ constraint/ # Generic type constraints
โ โโโ utils/ # Utility functions
โ โโโ examples/ # Usage examples
โ โโโ example1/ # Basic int enum
โ โโโ example2/ # String enum with validation
โ โโโ example3/ # Switch pattern enum
โ โโโ example4/ # Complex naming patterns
โโโ README.md
๐ License
MIT License. See LICENSE.
๐ค Contributing
Contributions are welcome! Report bugs, suggest features, and contribute code:
- ๐ Found a bug? Open an issue on GitHub with reproduction steps
- ๐ก Have a feature idea? Create an issue to discuss the suggestion
- ๐ Documentation confusing? Report it so we can improve
- ๐ Need new features? Share your use cases to help us understand requirements
- โก Performance issue? Help us optimize by reporting slow operations
- ๐ง Configuration problem? Ask questions about complex setups
- ๐ข Follow project progress? Watch the repo for new releases and features
- ๐ Success stories? Share how this package improved your workflow
- ๐ฌ General feedback? All suggestions and comments are welcome
๐ง Development
New code contributions, follow this process:
- Fork: Fork the repo on GitHub (using the webpage interface).
- Clone: Clone the forked project (
git clone https://github.com/yourname/repo-name.git).
- Navigate: Navigate to the cloned project (
cd repo-name)
- Branch: Create a feature branch (
git checkout -b feature/xxx).
- Code: Implement your changes with comprehensive tests
- Testing: (Golang project) Ensure tests pass (
go test ./...) and follow Go code style conventions
- Documentation: Update documentation for user-facing changes and use meaningful commit messages
- Stage: Stage changes (
git add .)
- Commit: Commit changes (
git commit -m "Add feature xxx") ensuring backward compatible code
- Push: Push to the branch (
git push origin feature/xxx).
- PR: Open a pull request on GitHub (on the GitHub webpage) with detailed description.
Please ensure tests pass and include relevant documentation updates.
๐ Support
Welcome to contribute to this project by submitting pull requests and reporting issues.
Project Support:
- โญ Give GitHub stars if this project helps you
- ๐ค Share with teammates and (golang) programming friends
- ๐ Write tech blogs about development tools and workflows - we provide content writing support
- ๐ Join the ecosystem - committed to supporting open source and the (golang) development scene
Happy Coding with this package! ๐
GitHub Stars
