Hacker News new | ask | show | jobs
by SamWhited 845 days ago
I generally agree that this is a big problem with Go, so I don't want to quibble too much, but the author acknowledges that the language doesn't have enums and that they're just trying to use this feature like enums (TBF, this is common advise on the internet and a lot of code does this): instead the author should be thinking "how do I solve this problem without enums since they don't exist?"

I'd be willing to bet that there's just a better way to do whatever the actual real-world example they want to achieve is (this was not entirely clear to me from the examples in the post).

Like I said though, that doesn't mean that (real) enums wouldn't be an even better way to do it than whatever the Go way is for a given problem, so I don't want to quibble too much since I think this is one of my biggest day-to-day complaints about Go, but it's worth pointing out that the premise can be flawed and that it's still a problem in the language, these two things aren't completely orthogonal.

TL;DR — Instead of pulling in a code generator and another library, it may be good to think of alternate ways to do the same thing without a lot of extra code footprint.

3 comments

>instead the author should be thinking "how do I solve this problem without enums since they don't exist?"

Which is exactly what they do in the post.

They still have every reason to complain about Go's oft suggested lame substitute.

> They still have every reason to complain about Go's oft suggested lame substitute.

Well, yeah, this is also the reason they deserve quite a bit of ridicule from actual Go users.

The author considered the Go pattern for “enums”. Found it lacking. Made their own code generator for their preferred pattern.

That’s two options. What are the others that are meaningfully different? You have to be able to deal with simple “sum types” in the sense of: this type could take on the value of one of these X predetermined constants. This requirement doesn’t disappear just because the language doesn’t directly support it.

I can't really think how this could be done.

Other languages either substitute enum with primitive type, string, or use strong type system tricks.

Go do duck typing, .. so..