...which makes it also very easy to read.
...which makes it also very easy to read.
To be more specific: the point isn't whether Go is "always" simpler to write but whether it is generally simpler to write. I think the latter is true for all the domains I've written code in Go and I've solved some pretty hard problems in Go (check out my Github repositories if you don't believe me).
The awesome thing about programming languages is that you're free to pick another language if that is a better fit for any specific domain or even personal preference. Hence why I've worked in more than a dozen different programming languages over the years. So rather than moaning that Go doesn't fit a particular pattern where you need to rely on generics, perhaps use a language that has generics to solve that problem and save Go for the occasions you don't need generics (which, if we're all honest, isn't something we need to rely on half as much as people claim when moaning about the lack of features in Go).
As a veteran developer, what really annoys me the most is people who pick a language that isn't well suited for solving the problem at hand and then moan that language needs to change, rather than developers picking the right tools for the job. I'd rather see a language lack features but focuses to serves its core demographic well than see everything and the kitchen sink is thrown at it and everyone using it even when they know they shouldn't simply because they're too afraid to learn anything new. What we need is more appreciation that "general purpose languages" doesn't mean "should be used to do practically anything in". Then I'd think we'd all get along better.
I find it especially powerful for writing tools.
That doesn't remove my rights to nitpick its shortcomings though.
I have kept a counter of many times I've told myself, over the past years, 'I wish Go had generics, this would really make my life easier right now'. It's currently at 5.
In my experience, the supposed issue of a lack of generics is _extremely_ overblown with regards to real world use. Sure, it would be nice to have them. I would've really liked them to exist a month ago when I was writing a generic container. But they're not going to make the language that much better for me, and there's always going to be more things PL people lust after (usually the next candidate would be GADTs, “Go does not have algebraic data types!” coming to HN comments very soon).
How about non-nullable types?
If I kept a similar counter for the past 3 years, it would be at least five per day.
Every time I `return x, err` and it's not a true tuple - and it almost never is - I wish I was returning an `Either<X, error>`. That's constant.
Every time I use atomic.Value (often) or sync.Pool (less often) or sync.Map (almost never because I end up rolling my own stupid map+mutex).
90% of the time I want to say a pointer can't be null. 100% of the time I want to handle optional JSON or SQL fields.
Fairly often, like > 30% of the time, when wrapping serdes or DTO conversions around data streams.
I don't believe the level of generics required to support these use cases requires compromising the other goals of Go. The deficiency should have been obvious as soon as they introduced `sync.Map`.
I can't imagine having this thought every single time. I don't want to sound harsh, but I don't know how else to ask this: can't you get over it?
Here are three options that were on the first page of google:
* https://github.com/kulshekhar/fungen
var out []X
for _, x := range elts {
if condition(x) {
out = append(out, x)
}
}
That’s so easy I typed it from my iPhone and anyone looking at it can infer at a glance that it’s filtering a list. If you are deeply worried about it, throw a comment at the top of the block.I too like terse code, but it’s because it makes me feel smart, not because it’s easier to read or more maintainable. I’m having a blast writing Rust for my hobby project, making everything super DRY and free from boilerplate, but I don’t pretend that I’m saving myself time or making things more readable or maintainable should someone else come across it.
In more cases than one the condition is a lambda-monstrosity shoved in the filter function, which makes it even harder to parse.
Note that the Python people have map/filter/reduce (and have had them for ages) and no one uses them. It’s either for loops or list comprehensions, and people notoriously struggle with the latter.
for { i <- 1 to 10; if i % 2 == 0 } yield i
but sometimes lambdas can be cleaner 1 to 10 filter { _ % 2 == 0 }The famous "if err != nil {" boilerplate becomes expected after each call and you only notice if it changes.
I'm saying this as someone who is learning go for fun.