Skip to content

Instantly share code, notes, and snippets.

@skarllot
Created June 18, 2015 14:04
Show Gist options
  • Save skarllot/102a5e5ea73861ff5afe to your computer and use it in GitHub Desktop.
Save skarllot/102a5e5ea73861ff5afe to your computer and use it in GitHub Desktop.
Enum-like types for Go (golang) that provides string representation
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})
}
@carloscasalar
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment