Most active commenters
  • NateDad(3)
  • waps(3)

←back to thread

131 points apta | 16 comments | | HN request time: 1.263s | source | bottom
1. barsonme ◴[] No.9266431[source]
His Go is a little disingenuous. This (https://gist.github.com/EricLagerg/105431503d32f18d239b) is almost as short as his D code, and functions the same.
replies(3): >>9266456 #>>9266519 #>>9267772 #
2. jff ◴[] No.9266456[source]
It's almost as if, like most people who write articles complaining about Go's lack of "expressiveness" and generics, he took a cursory look at the language, wrote some naive examples that supported his point, and squeezed out a blog post.
replies(2): >>9267073 #>>9275089 #
3. NateDad ◴[] No.9266519[source]
Use io.Copy to copy from the file to os.Stdout. no need to read the whole thing into memory.
replies(1): >>9266577 #
4. barsonme ◴[] No.9266577[source]
Hadn't even thought of that! Nice catch. It's even shorter: https://gist.github.com/EricLagerg/c3ccf96f6ed90d8b4557
replies(1): >>9268128 #
5. waps ◴[] No.9267073[source]
If the code was equivalent you might have a point. Unfortunately it is not (tokenizing/line scanning vs. copying). One has a shortcut in Go, the other does not.

Would you say that Go is not excessively verbose in non-trivial use cases ? I recently had to sort a struct list in a program. Added lines of code for sorting a single list once : 30. What the ...

replies(3): >>9267253 #>>9267258 #>>9267303 #
6. okbake ◴[] No.9267253{3}[source]
I'm curious about the specifics of your problem. I couldn't imagine sorting a list of structs (by one of the fields I presume) would be too terribly different in Go than in other langauges. Heres an example of insertion sort on a basic type: https://play.golang.org/p/SPoiNRVl2B

You can use the standard library sort methods by making your type implement the sort interface. Heres an example taken from the example in the sort docs: https://play.golang.org/p/oeRIhHi1Ei

replies(2): >>9267583 #>>9273646 #
7. NateDad ◴[] No.9267258{3}[source]
For anything larger than a trivial program, go is no more verbose than python. For small programs, there's too much variance to really say what language is less verbose, because you can always hit cases where one language has something in the stdlib and one doesn't.

The only place go is more verbose is sorting, and lack of map, filter, and list comprehension. Most of the latter just means you need a 3 line loop where you have one line in python. But that should not be a great effect on any reasonable size program.

replies(1): >>9268232 #
8. falcolas ◴[] No.9267303{3}[source]
There's an equivalent method which can be called to read individual lines: ReadLine. There's also join and split methods to more naturally model what the D code is actually doing.

    fileBytes, _ := ioutil.ReadAll(<file>)
    text := string(bytes.Join(bytes.Split(fileBytes, '\n')))
    fmt.Println(text)
> Added lines of code for sorting a single list once : 30. What the ...

Sorting requires implementing three methods, at least one of which is usually given to you for free (Len). A simple case will usually cost about 9 lines of neatly formatted code, 3 if you're not so neat (which the Go sort library does itself).

    func (sl structSlice) Len() int { return len(sl) }
    func (sl structSlice) Less(i, j int) bool { return sl[i].MyKey < sl[j].MyKey }
    func (sl structSlice) Swap(i, j int) { return sl[i], sl[j] = sl[j], sl[i] }
Ultimately, even this isn't a case of verbosity so much as it is not abstracting away the basic sorting functions.
replies(1): >>9267578 #
9. waps ◴[] No.9267578{4}[source]
Mind if I ask why these functions are laid out entirely differently from almost every other function in the go standard library ?

Yes, I know the Go authors do it too. And of course we know the answer : they're ashamed of just how bad it is, and mask it by making exceptions in the style guide for this specific case. You also left out the type declaration and the actual sorting call. Here's the full code :

  type myStructSlice []struct

  func (ms myStructSlice) Len() int {
    return len(ms)
  }

  func (sl myStructSlice) Less(i, j int) bool { 
    return sl[i].MyKey < sl[j].MyKey
  }

  func (sl myStructSlice) Swap(i, j int) {
    return sl[i], sl[j] = sl[j], sl[i]
  }

  ...
  sort.Sort(myStructSlice(sl))
And for this trivial example we're at 15 lines of (properly indented) code. You also have polluted the name space with one-use names for types. Is it really so hard to believe that this easily explodes to 30 lines of code for real life examples ? The Less function is not going to be nearly as simple in real life struct examples (natural sort + field precedence).

Compare to the same code in Java:

  Collections.sort(theList, (SomeType s1, SomeType p2) -> p1.field.compareTo(p2.field));
D:

  sort!("a.field < b.field")(theList)
C++:

  thelist.sort([](const someType& a, const someType& b) { return a.field < b.field; });
So Golang is 5x more verbose than Java in this instance. This a particularly bad example, but that Golang is actually more verbose than Java is not an exception in my experience.
10. waps ◴[] No.9267583{4}[source]
This post contains a comparison : https://news.ycombinator.com/item?id=9267578
11. f2f ◴[] No.9267772[source]
a few more lines if you just let os.Stdout be the default: http://play.golang.org/p/FDbpCxCskT
12. EugeneOZ ◴[] No.9268128{3}[source]
Stop it please, or PHP will win this race easily. With just 1 line of code. And it doesn't mean anything.
13. Paddy3118 ◴[] No.9268232{4}[source]
You can say that, but it doesn't make it so. Go here: http://rosettacode.org/wiki/Category:Go for many examples of more succinct Python with better commenting than the Go code, for example: http://rosettacode.org/wiki/Align_columns
replies(1): >>9275458 #
14. he_the_great ◴[] No.9273646{4}[source]
I see you've grabbed some examples to show the simplicity. Having to write the insertion sort algorithm to sort your basic types is not simple.

Having to tell the standard library how to swap data in a slice and to obtain a slices length is simple... why doesn't the standard library know how to work with basic types? This way I don't end up writing by accident:

    func (a ByAge) Swap(i, j int)      { a[i], a[j] = a[j], a[j] }
15. EvenThisAcronym ◴[] No.9275089[source]
From the very first line of the article:

>Over the course of the past few months I’ve been using Go to implement a proof of concept program in my spare time.

It's almost as if, like most people who post on Hacker News, you took a cursory look at the title, wrote some inane comment that supported your point, and squeezed out a reply.

16. NateDad ◴[] No.9275458{5}[source]
forgive the copy-paste response from elsewhere on thus thread.

an example from https://www.spacemonkey.com/blog/posts/go-space-monkey -

we decided to transliterate our 90k lines of Python directly to Go, line by line

The 90K number includes our tests, but without tests, the Python codebase is 36,784 lines of code. Those same lines of code became 41,717 lines of Go code. So, not that much of an increase. When you consider that's just 4,933 more lines, it's not crazy to assume most of those are closing braces.

I'd say closing braces and trivial expansions of list comprehensions into 3-5 line loops.

There's a real life example of direct transliteration, not just a rewrite. That's 13.4% more lines. I think that's pretty close.