Get the latest tech news

Enums in Go


Go does not have enums but there are ways to emulate the behavior. I'll walk you through a few examples.

If you run go generate ./... you’ll see a colors_string.go file appear, with the stringer interface implemented, allowing you to access the names of the members like so: For instance, you might be OK if the value is stored as an integer however if you’re exposing this information to the end user it might make sense to display the color name instead. You would obviously include ways to construct valid enums from outside the package by either the ID or name, the methods for serializing, stringifying and any other needs you have.

Get the Android app

Or read this on Hacker News

Read more on:

Photo of Enums

Enums