Go doesn't have enums like other languages, but we can make our own! Let's talk about best practises.
Hey folks! So in this video, we’re gonna learn about enums.
Enums are maybe not the most sexy thing to discuss, but Go doesn’t necessarily have enums directly. So it’s important to figure out, especially when you’re moving to Go from another language, what the equivalent is here, as well as talking about a couple of best practices.
There’s one thing you can do especially that’s gonna really save your bacon. Let me paste an example I wrote. As Go doesn’t have enums, one popular thing we do is use type constants.
Here, I’ve made a Weekday which is actually an int under the hood, but we’re gonna use it effectively like an enum. One thing we’ve done here is use something called iota.
You can see here what that is—it’s a pre-declared identifier representing the untyped integer number of the current const specification. It’s 0-indexed.
Now the reason we do it like this is if you do it in a block like this, it just increments automatically. You can see GoLand’s helping me figure out what the values are gonna be.
But one thing you’ve probably noticed I’ve done, which is a good practice that will save your bacon, is we should always use the zero value as an invalid value.
Let’s imagine this logic was really important to calculating the day of the week or whatever it is, and someone just didn’t actually set the value. It would say everything was Sunday if we used 0 to be Sunday.
So what you wanna do is always have zero as the invalid value. Whatever you’re trying to do with any sort of enumeration, use 0, and it will save you a lot of heartache in the future.
This is good advice in Go anyway—make the zero value useful and make setting these values deliberate. You’ll find you don’t go too far wrong if you follow that rule.
So we’ll run this just to see what happens, but I think you all can predict it. InvalidDay is 0, and Sunday’s value is 1.
Now we can take this one step further. What we can do is write a function like this where we switch on the Weekday. As you can see, I’ve made this as a receiver function, and we can switch and give the day back as a string.
Because we’ve called this on the String function, when we run this, we’re gonna see a slightly different result. Now we get Sunday’s name and InvalidDay’s name.
That’s because we’re using it in a string directive like this—it’s actually calling the String function.
One other thing you might want to do is something like this, where we have a switch statement where we can do different things depending on what values we’ve got. So if it’s Saturday or Sunday, we can say it’s the weekend, or we can say it’s a weekday.
Then we have a default case, which is probably gonna be a panic because we didn’t mean to handle it.
We can call our main function here, and you can see Monday’s a weekday, Sunday’s a weekend. In a situation where you pass something weird we don’t know what it is, it’ll hit that default case.
Some general advice for enums is to keep them grouped together. You can see at the top here we’ve got our days grouped together, but let’s say I was to make a new block or I wanted to make a new enum type.
By having separate blocks like this, it makes it very clear—these are related, and these are related.
So let’s just briefly summarize the lessons we’ve learned here. We can provide a String method to make this a lot clearer and easier to use.
We can use switch statements to very clearly do logic on our enumeration-style types here.
Most importantly, let’s use 0 for our invalid types just to make sure we don’t make any mistakes and accidentally introduce business logic we didn’t mean to.
Like anything else, write lots of tests, and you’ll keep yourself nice and safe.
I hope you found this lesson useful, and you now know a little bit more about how to use enums in Go—especially if you’re coming from a different language where support is a little bit more baked in, and you don’t have to reason about it as much as this.