Created
June 18, 2015 14:04
-
-
Save skarllot/102a5e5ea73861ff5afe to your computer and use it in GitHub Desktop.
Enum-like types for Go (golang) that provides string representation
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" | |
var enums []string | |
type Enum int | |
func (e Enum) String() string { | |
return enums[int(e)] | |
} | |
func ciota(s string) Enum { | |
enums = append(enums, s) | |
return Enum(len(enums) - 1) | |
} | |
var ( | |
Alpha = ciota("A") | |
Beta = ciota("B") | |
) | |
type Example struct { | |
X Enum | |
} | |
func main() { | |
fmt.Printf("%+v\n", Example{Alpha}) | |
fmt.Printf("%+v\n", Example{Beta}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://play.golang.org/p/HZ9u0C-_k22
It works, thanks