Skip to content

Instantly share code, notes, and snippets.

@nirbhayc
Created June 20, 2017 21:26
Show Gist options
  • Save nirbhayc/a36225e71d535f2502875e52a93b34bc to your computer and use it in GitHub Desktop.
Save nirbhayc/a36225e71d535f2502875e52a93b34bc to your computer and use it in GitHub Desktop.
(Blog) Enum type in Go
package main
import "fmt"
type myColor string
const (
RED myColor = "red"
GREEN myColor = "green"
BLUE myColor = "blue"
)
func PrintColor(color myColor) {
fmt.Println(color)
}
func main() {
PrintColor(RED)
PrintColor(BLUE)
// Compile error : undefined: BLACK
// PrintColor(BLACK)
// Compile error : cannot use 20 (type int) as type myColor in function argument
// PrintColor(20)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment