Hacker News new | ask | show | jobs
by BugsJustFindMe 844 days ago
> Go doesn’t technially have Enums and it is a missing feature in my book but there is a Go idiomatic way to achieve roughly the same thing.

Oh, so it's a lot like Python then.

> This is fine however it is nothing but an integer under the covers this means what we actually have is:

Oh, so it's a lot like C++ then.

> But what you notice here is we have no string representation of these Enums so we have to build that out next

Have to!

Yes this still all sucks. (But at least there's ugly historical precedent!)

3 comments

Go is in this weird middle ground where it's modeled after C, so it's got things like no enums, return codes for errors, mutable everything, nulls, and pointers (that don't support arithmetic, so it's really just this "*" sigil that you have to remember to use sometimes), but it's also fully garbage collected and has built-in, stackful green threads. I have no idea what it's actually trying to be.
It's all of the ergonomics of C combined with the bare metal performance of a garbage collector.
and the verbosity of very old java
I would wager that the vast majority of backend software jobs are for people writing REST API microservices, exchanging JSON, with a mindset that is more practical and "blue-collar" than academic.

Golang is an absolutely ideal language for writing REST API microservices, that exchange JSON, with a practical and blue-collar mindset.

Plus it compiles to small-ish native executables. Which renders Docker superfluous in many uses cases, and also makes it well-suited for writing DevOps tooling (e.g. Docker, everything from HashCorp, etc).

It's not trying to out-cool Haskell and Rust on online message boards. But I would never in a million years evangelize either of those two languages for routine REST API work in most real-world shops, whereas I could suggest Golang without losing professional credibility.

It’s funny to emphasize the vibes of a language contra other ones when you’re supposed to be on the supposedly pragmatic side. Haskell and Rust are popular on “message boards”? Better not mention them among my peers and risk my blue collar street cred.

But it’s a red herring in any case since enum types are such a basic programming language feature. No need to evoke the Cool Kids languages at all.

Completely true. And most of the time, I enjoy writing go. But the enums are weak. However, if the Go team wants to tackle an important addition to the language, it should be non-nillable pointers. No need for Option sum types, just a type annotation that says it cannot be nil (but can be assigned a nil type if you've tested it for not being nil). I've thought about building a linter (like the Uber one), but the way the types in the syntax tree are resolved makes it too complex for a small project.
> Golang is an absolutely ideal language for writing REST API microservices

Those are strong words for a language with all the flaws I just mentioned. :D Yes, green threads are great for network programming, but it's not the only language with them, and one feature does not make it "ideal". If I had to pick the best networking language... I'd probably say Elixir.

But even if we agree that it's ideal, it doesn't change my point.

I find go distasteful, but are there really many other languages with an m:n threading model? Only other popular one I can think of is Erlang/Elixir.
Java 21, and I assume like every scripting language (Ruby, Python, etc). Though I guess with scripting you can't use more than one OS thread (not totally sure). Rust started off with it, and C# tried it too, but there are huge downsides to the model, so it's not like it's perfection incarnate and every other language just can't pull it off.
Does 21 actually automatically schedule fibers? to open cores?
Elixir performance is pretty average and it's not a statically typed language, things will blow up at runtime.
The better language at this to both Java, Go and, god forbid, Elixir is C#. It properly implements generics, pattern matching and task-based asynchronous model which allows to trivially interleave or dispatch all kinds of method calls in ways which require extremely verbose and bulky code in Go.
completely agree Elixir is a much better language all around for this type of work
More efficient than a scripting language, less performant than a systems language. I really like it for anything network-related.
C does have enums. Not trying to detract from your point, which I agree with, but enums are definitely a thing, which C has.
> C does have enums.

Then again, so does Go. Go doesn't have an enum keyword like C, but that isn't what defines enums.

But neither C nor Go have type-safe enums.
They are type safe. They are not value safe, but neither language supports value constraints, so that isn't unexpected.
What is value safety? Why should value constraints be pertinent here? near I can tell this is a neologism (introduced here? https://itnext.io/we-need-to-talk-about-the-bad-sides-of-go-...) that just happens to be near the top of search results in this area.

The introduction rule for enum values in C is _not_ type safe. You know how you can tell? Well typed programs go wrong. a language absolutely does not need value constraints of any kind to get this right.

Python does have enums in its standard library

https://docs.python.org/3/library/enum.html

Heh. Python's enums are not real types, they're just funny classes, so you still have to do dumb things like assign an internal value (commonly a dumb int) and were (and probably still are) wildly deficient in many other commonly-desired ways for a very long time. A bunch of things covered in this blog post (like StrEnum and EnumCheck) weren't added until pretty recently.
C++ added enum classes a while back, though.