Skip to content

Instantly share code, notes, and snippets.

@podanypepa
Last active Aug 31, 2020
Embed
What would you like to do?
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