go-enum icon indicating copy to clipboard operation
go-enum copied to clipboard

Struct based enums

Open kirillkuzin opened this issue 9 months ago • 1 comments

Hey, I haven't found any way to generate struct based enums that can be used to avoid assigning incorrect values to variables of enum type. I guess this could be a useful improvement, what do you think? The example of enum I'm talking about:

type Role struct {
	slug string
}

func (r Role) String() string {
	return r.slug
}

var (
	Unknown   = Role{""}
	Guest     = Role{"guest"}
	Member    = Role{"member"}
	Moderator = Role{"moderator"}
	Admin     = Role{"admin"}
)

kirillkuzin avatar Mar 02 '26 21:03 kirillkuzin

I don't really follow the struct based enum line of thought, and haven't really ever felt the need or desire to move in that direction. The go team provided the iota as a way to support these types, and this package is built on top of that idea.

I think there are other providers that support struct based enums if you need help with doing that.

I appreciate your interest in the project and hope you find what you're looking for. 🫶

abice avatar Mar 03 '26 01:03 abice