Last active
August 31, 2020 09:56
-
-
Save podanypepa/e11d9c97d720548e7972be7c242f1580 to your computer and use it in GitHub Desktop.
Enums in Golang
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 | |
import ( | |
"fmt" | |
) | |
type COLOR int32 | |
const ( | |
BLACK COLOR = iota | |
BLUE | |
RED | |
) | |
var ColorName = map[COLOR]string{ | |
BLACK: "black", | |
BLUE: "blue", | |
RED: "red", | |
} | |
var ColorValue = map[string]COLOR{ | |
"black": BLACK, | |
"blue": BLUE, | |
"red": RED, | |
} | |
func (x COLOR) String() string { | |
return ColorName[x] | |
} | |
func main() { | |
fmt.Println("by const:", RED) | |
fmt.Println("by key:", COLOR(3)) | |
fmt.Println("by value", ColorValue["black"]) | |
fmt.Printf("enum: %d \n", BLUE) | |
fmt.Printf("enum string: %s \n", BLUE) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment