goenum

package module
v0.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 10, 2026 License: MIT Imports: 1 Imported by: 0

README

GitHub Workflow Status (branch) GoDoc Coverage Status Supported Go Versions GitHub Release Go Report Card

GOENUM

Go enum code generation that enables different business domains to share common enum names like OK, ERROR, PENDING without naming conflicts through namespace isolation.


CHINESE README

中文说明

Features

🔒 Namespace Isolation - Each domain has its own enum space, preventing naming conflicts ⚡ Enum Collection - Generated Enums() returns collection with Lookup, List, Get methods 🎯 Clean Code - Intuitive syntax that matches business logic patterns ✅ Compile Protection - Catch enum misuse at build time, not runtime 🌍 Multi-Language - Generate enums using various language characters

Installation

go get github.com/yylego/goenum

Usage

Go lacks true enum namespaces. Different domains can't share common value names like OK, ERROR, PENDING.

Before: Verbose Prefixes Required
type PackageStatus string
const (
    PackagePending   PackageStatus = "PENDING"
    PackageConfirmed PackageStatus = "CONFIRMED"
    PackageShipped   PackageStatus = "SHIPPED"
    PackageDelivered PackageStatus = "DELIVERED"
)

type PaymentStatus string
const (
    PaymentPending PaymentStatus = "PENDING"
    PaymentFailed  PaymentStatus = "FAILED"
    PaymentSuccess PaymentStatus = "SUCCESS"
    PaymentRefund  PaymentStatus = "REFUND"
)
// Verbose switch statements with long prefixes
func processPackage(status string) {
    switch PackageStatus(status) {
    case PackagePending:
        // handle pending
    case PackageConfirmed:
        // handle confirmed
    case PackageShipped:
        // handle shipped
    case PackageDelivered:
        // handle delivered
    }
}
func processPayment(status string) {
    switch PaymentStatus(status) {
    case PaymentPending:
        // handle pending
    case PaymentFailed:
        // handle failed
    case PaymentSuccess:
        // handle success
    case PaymentRefund:
        // handle refund
    }
}
With GOENUM: Clean Namespace Methods
// Each domain gets its own clean namespace
func processPackage(status string) {
    switch PackageStatusEnum(status) {
    case PackageStatus.Pending():
        // handle pending
    case PackageStatus.Confirmed():
        // handle confirmed
    case PackageStatus.Shipped():
        // handle shipped
    case PackageStatus.Delivered():
        // handle delivered
    }
}
func processPayment(status string) {
    switch PaymentStatusEnum(status) {
    case PaymentStatus.Pending():
        // handle pending
    case PaymentStatus.Failed():
        // handle failed
    case PaymentStatus.Success():
        // handle success
    case PaymentStatus.Refund():
        // handle refund
    }
}
Enum Validation with Enums()

Each generated enum type has an Enums() method returning a collection that supports validation and lookup:

// Validate enum value
if _, ok := PackageStatus.Enums().Lookup(status); !ok {
    return errors.New("invalid package status")
}

// Get all valid enum values
allStatuses := PackageStatus.Enums().List()

Multi-Language Support

GOENUM supports enum generation in multiple languages:

// Chinese enum usage example
func processTask(status string) {
    taskStatus := TaskStatusEnum(status)
    switch taskStatus {
    case TaskStatus.C待处理():
        // handle pending task
    case TaskStatus.C已确认():
        // handle confirmed task
    case TaskStatus.C进行中():
        // handle active task
    case TaskStatus.C已完成():
        // handle completed task
    }
}
// Traditional Chinese enum example
func processPermission(status string) {
    permStatus := PermissionStatusEnum(status)
    switch permStatus {
    case PermissionStatus.C開啟():
        // handle enabled permission
    case PermissionStatus.C關閉():
        // handle disabled permission
    }
}
// Japanese enum example
func processConnection(status string) {
    connStatus := ConnectionStatusEnum(status)
    switch connStatus {
    case ConnectionStatus.C接続():
        // handle connected
    case ConnectionStatus.C切断():
        // handle disconnected
    case ConnectionStatus.C待機():
        // handle waiting
    }
}
// Korean enum example
func processGame(status string) {
    gameStatus := GameStatusEnum(status)
    switch gameStatus {
    case GameStatus.C시작():
        // handle game start
    case GameStatus.C종료():
        // handle game end
    case GameStatus.C일시정지():
        // handle game pause
    }
}

Examples: See examples


  • enum - Go enum collection management with metadata support and map-based lookup
  • goenum - Go enum code generation with namespace isolation (this package)
  • protoenum - Protocol Buffers enum code generation with type-safe operations

📄 License

MIT License - see LICENSE.


💬 Contact & Feedback

Contributions are welcome! Report bugs, suggest features, and contribute code:

  • 🐛 Mistake reports? Open an issue on GitHub with reproduction steps
  • 💡 Fresh ideas? Create an issue to discuss
  • 📖 Documentation confusing? Report it so we can improve
  • 🚀 Need new features? Share the use cases to help us understand requirements
  • Performance issue? Help us optimize through reporting slow operations
  • 🔧 Configuration problem? Ask questions about complex setups
  • 📢 Follow project progress? Watch the repo to get new releases and features
  • 🌟 Success stories? Share how this package improved the workflow
  • 💬 Feedback? We welcome suggestions and comments

🔧 Development

New code contributions, follow this process:

  1. Fork: Fork the repo on GitHub (using the webpage UI).
  2. Clone: Clone the forked project (git clone https://github.com/yourname/repo-name.git).
  3. Navigate: Navigate to the cloned project (cd repo-name)
  4. Branch: Create a feature branch (git checkout -b feature/xxx).
  5. Code: Implement the changes with comprehensive tests
  6. Testing: (Golang project) Ensure tests pass (go test ./...) and follow Go code style conventions
  7. Documentation: Update documentation to support client-facing changes
  8. Stage: Stage changes (git add .)
  9. Commit: Commit changes (git commit -m "Add feature xxx") ensuring backward compatible code
  10. Push: Push to the branch (git push origin feature/xxx).
  11. PR: Open a merge 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 via submitting merge 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

Have Fun Coding with this package! 🎉🎉🎉


GitHub Stars

Stargazers

Documentation

Overview

Package goenum: Go enumeration code generation and type safe enum utilities Auto generates enum types with compile-time type safety guarantees Integrates with github.com/yylego/enum package for collection management Enables efficient enum validation with map-based lookup

goenum: Go 枚举代码生成和类型安全枚举工具包 自动生成具有编译时类型安全保证的枚举类型 与 github.com/yylego/enum 包集成进行集合管理 实现基于 map 查找的高效枚举验证

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Enum

type Enum[enumCode comparable] = enum.Enum[enumCode, *enum.MetaNone]

Enum is a type alias for enum.Enum with MetaNone metadata Simplifies the generic signature from two type params to one

Enum 是 enum.Enum 的类型别名,使用 MetaNone 元数据 将泛型签名从两个类型参数简化为一个

func NewEnum

func NewEnum[C comparable](code C) *Enum[C]

NewEnum creates a new Enum instance with Go native enum Wraps enum.NewEnum for convenience

创建新的 Enum 实例,绑定 Go 原生枚举 封装 enum.NewEnum 以便使用

type Enums

type Enums[enumCode comparable] = enum.Enums[enumCode, *enum.MetaNone]

Enums is a type alias for enum.Enums with MetaNone metadata Simplifies the generic signature from two type params to one

Enums 是 enum.Enums 的类型别名,使用 MetaNone 元数据 将泛型签名从两个类型参数简化为一个

func NewEnums

func NewEnums[C comparable](params ...*Enum[C]) *Enums[C]

NewEnums creates a new Enums collection from the given Enum instances Wraps enum.NewEnums for convenience

从给定的 Enum 实例创建新的 Enums 集合 封装 enum.NewEnums 以便使用

Directories

Path Synopsis
Package goenumgen: Go enum code generation engine with flexible naming modes Auto generates type safe enum code with customizable naming patterns Returns enum.Enums collection for validation and lookup operations Provides compile-time safe checks with runtime performance optimization
Package goenumgen: Go enum code generation engine with flexible naming modes Auto generates type safe enum code with customizable naming patterns Returns enum.Enums collection for validation and lookup operations Provides compile-time safe checks with runtime performance optimization
internal
utils
Package utils: Code generation string handling utilities Provides string formatting and quoting functions for code generation Supports type conversion and quote manipulation for generated code
Package utils: Code generation string handling utilities Provides string formatting and quoting functions for code generation Supports type conversion and quote manipulation for generated code

Jump to

Keyboard shortcuts

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