Hacker News new | ask | show | jobs
by IshKebab 3596 days ago
It may be harder to write some things, but it definitely is easier to read Go code.

Besides, while the language itself may be more verbose than it could be, the standard library is extremely pragmatic and terse. It's like the opposite of the standard C++ library. E.g. to see if a string starts with another string in C++:

    std::mismatch(prefix.begin(), prefix.end(), toCheck.begin()).first == prefix.end()
In Go:

    strings.HasPrefix(toCheck, prefix)
The Go standard library is full of things that do exactly what you want them to, whereas in other languages you have to manually do it yourself.
5 comments

In your C++ example you're giving `std::mismatch`, which is a generic algorithm. If you're so inclined you could provide a wrapper that has the same interface as the Go example, but you're comparing apples to oranges. I'd argue that `std::mismatch` is much _more_ pragmatic than the Go example, in that I can use it to check any lists of user defined types.

In reality, these two methods do completely different things. `std::mismatch` is a completely generic algorithm that 'returns the first mismatching pair of elements from two ranges', which can be used for much more than `strings.HasPrefix`.

Did you read the article? The fact that there is 1 way to do it in Go, and 100 different ways to do it in C++ (or some other older language) is a feature, not a bug.
Where are the 99 other ways? I see one way to do this for any set of data types. Golang is the one with 100 implementations.
> The Go standard library is full of things that do exactly what you want them to, whereas in other languages you have to manually do it yourself.

Sorting a slice is a pretty obvious counterexample.

Or checking for the presence of an item in a slice. Because Go doesn't supply sets, or allow you to write them yourself, this is something i find myself needing to do a lot, and every time, i'm writing that idiotic function from scratch.
Dang, just like PHP (no built in set)
And yet there is a generic in array function.
...which is O(n). Unless the set is trivially small, better to coerce your data to key types and use array_key_exists().
I never said it was good, but it exists. Most of my PHP code utilizes kv arrays everywhere possible.
You can use map[X]bool as a set. Not ideal I agree, but it works ok.
> The Go standard library is full of things that do exactly what you want them to, whereas in other languages you have to manually do it yourself.

No.

Python: toCheck.startswith(prefix)

JavaScript: toCheck.startsWith(prefix)

Haskell: prefix `isPrefixOf` toCheck

What other languages does Go compete with that don't have a "string starts with" in their standard library.

Since I feel I've proven my point about all other languages Go competes with having this function, what other helpful functions do you think Go has that it's competitors do not?

  to see if a string starts with another string in C++
As others have pointed out, while a person could use

  std::mismatch
C++'s std::basic_string type has a compare method. So, the Go example you presented:

  strings.HasPrefix(toCheck, prefix)
Could be expressed in Standard C++ as:

  toCheck.compare (0, prefix.length (), prefix) == 0;
Oh wow, string prefix checking is the example you want to run with? In C#:

  toCheck.StartsWith(prefix)
Meanwhile in Golandia, this is still an issue:

https://github.com/golang/go/issues/16721#issuecomment-24015...

...and I haven't felt this kind of pinch when using C#, ever. But what do I know? I'm just a .NET wage-slave pleb who's too mentally handicapped to see Go's glory.

It was just a simple example. Of course many languages have that. The point is Go nearly always has what you want.

Don't pretend that there aren't things in C# that are better in Go.

And I agree, the min/max thing is stupid. I never said Go was perfect.

Not being a Gopher myself, and seeing whats missing from Go compared to C#, I don't see why I'd ever bother using it (unless it's a work requirement).

I like generics and the things that come with it, like LINQ and manipulating abstract collections in a type-safe manner, thank you very much :)