Skip to content

Instantly share code, notes, and snippets.

@podanypepa
Last active August 31, 2020 09:56
Show Gist options
  • Save podanypepa/e11d9c97d720548e7972be7c242f1580 to your computer and use it in GitHub Desktop.
Save podanypepa/e11d9c97d720548e7972be7c242f1580 to your computer and use it in GitHub Desktop.
Enums in Golang
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