Idiomatic Enums in Go

A programming idiom is the usual way to code a task in a specific language. Although Golang does not have built in support for enumerations there is an idomatic way to emulate the feature. The idiomatic way to have enumerations in Go is to use a combination of the Iota and Integer types. type TaxonomicRank int const ( Animalia TaxonomicRank = iota Plantae Fungi Protista Archaea Bacteria ) func (m TaxonomicRank) String() string { return toString[m] } var toString = map[TaxonomicRank]string{ Animalia: "Animalia", Plantae: "Plantae", Fungi: "Fungi", Protista: "Protista", Archaea: "Archaea", Bacteria: "Bacteria", } var toEnum = map[string]TaxonomicRank{ "Animalia": Animalia, "Plantae": Plantae, "Fungi": Fungi, "Protista": Protista "Archaea": Archaea "Bacteria": Bacteria, } For extra credit, as they say, we can add support for serialization support for our enum with the following...

June 19, 2021