3 ms·
Writing Go it is good practice dealing with the errors first and, when possible, using if/return; and I agree. Less indentation is good IMHO, and conditional ne
by reidrac 9y ago
Writing Go it is good practice dealing with the errors first and, when possible, using if/return; and I agree. Less indentation is good IMHO, and conditional nesting is usually best if avoided.
Working on someone's else code I've found this:
if a, err := func(); err != nil {
return err
} else {
// a exists here
...
}
// a is gone
I would have wrote it differently:
a, err := func()
if err != nil {
return err
}
// work with a
I guess it depends on the amount of code to put in the "else" branch, but I thought this was interesting because it looks like someone was avoiding to write the error checking in a different line.