(no title)
blindseer | 2 years ago
err := db.Get(&latLong, "SELECT lat, long FROM cities WHERE name = $1", name)
if err == nil {
return latLong, nil
}
latLong, err = fetchLatLong(name)
if err != nil {
return nil, err
}
err = insertCity(db, name, *latLong)
if err != nil {
return nil, err
}
In Rust propagating errors is a lot more succinct and easy to do. It is usually what you want to do as well (you can think of Python and C++ exceptions as essentially propagating errors). The special case can be handled explicitly. In Go, you have to handle everything explicitly, and if you don't you can fail catastrophically.I guess it comes down to what features the language provides that makes it easy to do "the right thing" (where "the right thing" may depend on where your values lie; for example, I value correctness, readability of domain logic, easy debugging etc). And in my opinion, it's easy to do what I consider bad software engineering in languages like Go.
bheadmaster|2 years ago
Go errors usually contain enough context that they're good enough to print to console in CLI applications. Docker does exactly that - you've seen one if you've ever tried executing it as a user that isn't in "docker" group:
Yoric|2 years ago
Whether that's better or worse than the Go snippet is something I'll let the reader decide.
blindseer|2 years ago
Not to pick on any of these projects, but this pattern is way too common to not have a some sugar:
https://github.com/goodlang/good/blob/3780b8d17edf14988777d3...
https://github.com/kubernetes/kubernetes/blob/1020678366f741...
https://github.com/golang/go/blob/6cf6067d4eb20dfb3d31c0a8cc...