Skip to content

Instantly share code, notes, and snippets.

@saginadir
Last active December 15, 2020 20:41
Show Gist options
  • Save saginadir/d818312480d300e5d0996f4425eb049b to your computer and use it in GitHub Desktop.
Save saginadir/d818312480d300e5d0996f4425eb049b to your computer and use it in GitHub Desktop.
Golang robust enum
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