Hacker News new | ask | show | jobs
by _bpo 4234 days ago
I didn't mean to imply Go invented automatic formatting :) It's unique among the popular language that I know in that the formatter is built into the standard toolkit. It also goes well beyond indentation. I've never seen a discussion of the format of go code outside the discussion of how `go fmt` should work - this I consider a benefit.
2 comments

If you consider C++'s standard toolkit to be Clang, Clang comes with clang-format.
indent goes well beyond indentations too, despite the name :)
You're certainly correct, but if you compare this:

  http://linux.die.net/man/1/indent
with this:

  https://golang.org/cmd/gofmt/
even just by line count, you'll see the difference in philosophy.
Unfortunately having a code formatter does not make up for basic deficiencies in the language itself when it comes to datatypes. Basic example :

http://stackoverflow.com/questions/19946992/sorting-a-map-of...

(shortest way to sort an array of a custom datatype in go is around 50 lines of code, and requires you to write a custom sorting class)

First example on that page shows a straightforward solution in under 10 lines of code.

Convert the map values to a slice and then use the stdlib package sort to organize the data.

You left out defining a sorting class, like in Turbo Pascal. So, it's 17 lines of code (wrong version provided on stackoverflow) or 21 lines, the correct version :

  type dataSlice []*data

  // Len is part of sort.Interface.
  func (d dataSlice) Len() int {
      return len(d)
  }

  // Swap is part of sort.Interface.
  func (d dataSlice) Swap(i, j int) {
      d[i], d[j] = d[j], d[i]
  }

  // Less is part of sort.Interface. We use count as the value to sort by
  func (d dataSlice) Less(i, j int) bool {
      // WRONG : will crash if there's a nil in the list ...
      return d[i].count < d[j].count

      // Correct version
      if d[i] == nil {
          return true // note : true is an exported name, with no capital ... yet another inconsistency
      }
      if d[j] == nil {
          return false
      }
      return d[i].count < d[j].count
  }

  func main() {
    // create s of type []*data

    sort.Sort(s)
  }
Python version:

  // create list of data in variable s
  s.sort(key=lambda x: x.count)
C++ version:

  // Create Vector<Data> in s
  sort(s.begin(), s.end(), [](const Data& d1, const Data& d2) { return d1.count < d2.count; });

  (bonus for the C++ version : it's behavior is defined and correct for nulls, just saying this because none of the other examples are, including the Go one)
Java version:

  // Create List<Data> in s
  Collections.sort(s, (Data d1, Data d2) -> Integer.compare(d1.count, d2.count));
Let's put it this way. If Java is 16 TIMES more concise than your language, you have a problem. A big problem.