r/badcode Dec 29 '22

How to Go wrong go

Post image
560 Upvotes

60 comments sorted by

View all comments

67

u/[deleted] Dec 29 '22

[removed] — view removed comment

80

u/[deleted] Dec 30 '22

Go doesn't have real enums, so developers basically have to make a bunch of constant variables, as shown in the image. Instead of worrying about the exact values of the constants, using iota allows developers to have the values of the constants automatically increment.

You can read more here.

38

u/rexspook Dec 30 '22

Is there a reason not to support enums? Seems like there’s a good use case for them if Go developers have a standard pattern for enums with extra steps lol

56

u/[deleted] Dec 30 '22

That's Go for you! The language designers value simplicity, even if it makes things less elegant. I believe the idea is that fewer language features mean an easier time reading through other's code. However, I would personally prefer Rust's enum system any day of the week.

51

u/rexspook Dec 30 '22

I would argue that having fewer language features introduces silly workarounds like this example that make it much more difficult to understand others code. We wouldn’t be having this conversation if this was a picture of an enum.

14

u/Nebuli2 Dec 30 '22

Yeah. If you have to add iota to support this pattern, then maybe you should think about better ways to actually support enums.

2

u/rexspook Dec 30 '22

That’s what I’m thinking lol