Go enum code generation that enables different business domains to share common enum names like OK, ERROR, PENDING without naming conflicts through namespace isolation.
๐ 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
go get github.com/yyle88/goenumGo lacks true enum namespaces. Different domains can't share common value names like OK, ERROR, PENDING.
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
}
}// 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
}
}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()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
MIT License - see LICENSE.
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
New code contributions, follow this process:
- Fork: Fork the repo on GitHub (using the webpage UI).
- 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 the changes with comprehensive tests
- Testing: (Golang project) Ensure tests pass (
go test ./...) and follow Go code style conventions - Documentation: Update documentation to support client-facing changes
- 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 merge request on GitHub (on the GitHub webpage) with detailed description.
Please ensure tests pass and include relevant documentation updates.
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! ๐๐๐