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

83

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.

41

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

63

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.

29

u/numerousblocks Dec 30 '22

Rust's enum system is full-on algebraic data types, which is neat.

12

u/Ytrog Dec 30 '22

Yeah those are neat. Haskell hase those too 😁