Created
June 20, 2017 21:26
-
-
Save nirbhayc/a36225e71d535f2502875e52a93b34bc to your computer and use it in GitHub Desktop.
(Blog) Enum type in Go
This file contains hidden or 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 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