4 ms·
Go's strong decisions around formatting (`go fmt`) are another example of planning for enterprise-size. There is no need for a style guide for go programs - it'
by desdiv 12y ago
Go's strong decisions around formatting (`go fmt`) are another example of planning for enterprise-size. There is no need for a style guide for go programs - it's built into the language, and with an approach that feels less pedantic than Python.
How is that different from the 40 years old Unix utility indent? Pretty much every single non-trivial programming language has its own code formatter. Auto code formatting is neither novel nor unique to Go.
- _bpo 12y agoI 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.
- desdiv 12y agoindent goes well beyond indentations too, despite the name :)
- _bpo 12y agoYou'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.
- waps 12y agoUnfortunately 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-structs-golang 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)
- Injunire 12y agoFirst 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.
- waps 12y agoYou 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.
- nostrademons 12y agoIf you consider C++'s standard toolkit to be Clang, Clang comes with clang-format.