Created
November 23, 2020 14:52
-
-
Save cpl/4224ff9818055dcd4ff6522a3db8d139 to your computer and use it in GitHub Desktop.
Go Enums Quick Example
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 example | |
type AnimalType uint8 | |
const ( | |
AnimalTypeUnknown AnimalType = iota | |
AnimalTypeDog | |
AnimalTypeCat | |
AnimalTypeRabbit | |
AnimalTypeSnake | |
AnimalTypeJellyfish | |
) | |
var animalTypeStringMap = map[AnimalType]string{ | |
AnimalTypeUnknown: "unknown", | |
AnimalTypeDog: "dog", | |
AnimalTypeCat: "cat", | |
AnimalTypeRabbit: "rabbit", | |
AnimalTypeSnake: "snake", | |
AnimalTypeJellyfish: "jellyfish", | |
} | |
func (a AnimalType) String() string { | |
str, ok := animalTypeStringMap[a] | |
if ok { | |
return str | |
} | |
return "" | |
} | |
// func (a AnimalType) Scan ... | |
// func (a AnimalType) Marshal / Unmarshal |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment