Last active
December 15, 2020 20:41
-
-
Save saginadir/d818312480d300e5d0996f4425eb049b to your computer and use it in GitHub Desktop.
Golang robust enum
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 enums | |
// < ----- This is your core template | |
type CatEnum interface{ | |
enum() catEnumStruct | |
} | |
type catEnumStruct struct {} | |
func (c catEnumStruct) enum() catEnumStruct { | |
return c | |
} | |
// -----> | |
// ----- Below are your enums which you can access publicly outside of the enum package ----- | |
var ( | |
CatEnumStreet = catEnumStruct{} | |
CatEnumTabby = catEnumStruct{} | |
CatEnumUnknown = catEnumStruct{} | |
) | |
/* | |
Additionally, you must structure your project as follows: | |
/mypackage | |
/enums/cat_enum.go | |
The cat enum must be it's own package to avoid access to the private catEnumStruct to avoid implementing | |
additional types who can match the public CatEnum interface | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment