Last active
April 30, 2022 10:54
-
-
Save donchev7/834f38096b29750ab07f04c3989becec to your computer and use it in GitHub Desktop.
Doing enums in go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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