Go enum generator
go get github.com/phelmkamp/magnum
-
Define a simple struct type
-
Enumerate values in the struct tag of the
name
fieldFormat is
enum:"value[,value2]"
:type Color struct { name string `enum:"red,orange,yellow,green,blue,indigo,violet"` }
-
(Optional) Enumerate values of other fields with additional struct tags (see house.House)
-
-
Run command
magnum --path=$SRCDIR
Better yet, add the following comment to a file at the root of your source tree (e.g. main.go) and run
go generate
as part of your build process//go:generate magnum
-
That's it!
An *_enum.go file is generated for each *.go file that has enum tags. This file contains accessors for the enumerated values plus methods to marshal/unmarshal a value to/from text (and JSON).