by pothibo · 11y ago · 0 comments
The amount of for loops you will write will also makes it obvious when you start doing O(n^2) operations in your methods. Same can be said with variables declaration and error verifications in Go. They are annoying at first, but then you understand by reading your code the places where error can occur or where you decided to ignore errors (By using _).
It's a different approach and it's refreshing.
This compiles:
package main
import "errors"
func main() {
f()
println("hello erroneous world")
}
func f() error { return errors.New("boo") }This compiles:
package main
import "errors"
func main() {
x, err := f()
if err != nil {
return
}
y, err := g()
h(x, y)
}
func f() (x int, err error) { return 1000, nil }
func g() (y int, err error) { err = errors.New("boo"); return }
func h(x, y int) { println(x/y) }
(I do write Go most days and rather like it, but apologists for its weak parts are legion)In any case, the go compiler is very helpful in most cases, and better tooling can improve things further.
Either way, a very good reason for not using loops is to make code more readable. If I see a loop I have to run it inside my head to figure out what the intent is, and if there's multiple things going on, that can take time. If I see a 'map' I think "right, this is tranforming every element in this list in this way"; if I see a filter, I think "right, this is removing elements in this list that don't satisfy this property", if I see a groupBy, I think "right, this is grouping elements in this list by this property", etc., etc.
Code isn't only more succinct, it's more human friendly.
Unless you need a do/while loop. Then you're SOL.
Not having generics means we can't properly build these ourselves, and Go doesn't offer list comprehensions, so instead we're forced to for loop.
I used to play with a small plastic hammer when I was in
kindergarden thirty years ago!
I will be very irritated if people laugh at me for trying
to build my new house with this hammer now.
-- Golangers