Skip to content

Instantly share code, notes, and snippets.

@donchev7
Last active April 30, 2022 10:54
Show Gist options
  • Save donchev7/834f38096b29750ab07f04c3989becec to your computer and use it in GitHub Desktop.
Save donchev7/834f38096b29750ab07f04c3989becec to your computer and use it in GitHub Desktop.
Doing enums in go
package main
type role uint
const (
Unknown role = iota
Guest
Member
Moderator
Admin
)
type Role interface {
Code() uint
Is(Role) bool
Slug() string
}
func (r role) Code() uint {
return uint(r)
}
func (r role) Is(cm Role) {
return r.Code() == cm.Code()
}
func (r role) Slug() string {
switch r {
case Guest:
return "guest"
case Member:
return "member"
case Moderator:
return "moderator"
case Admin:
return "admin"
}
return ""
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment