6 ms·
>With Go's multiple return values to represent errors, they are also known to be too easy to "forget" to even look at the error value. How? Unassigned foo
by kirici 1y ago
>With Go's multiple return values to represent errors, they are also known to be too easy to "forget" to even look at the error value.
How?
Unassigned
foo := myFunc() # [...] assignment mismatch: 1 variable but myFunc returns 2 values
Assigned, but not used
foo, bar := myFunc()
fmt.Println(foo) # [...] declared and not used: bar
Intentionally ignored
foo, _ := myFunc()
Ignoring is a deliberate decision and trivial to review, lint and grep for.
Except for, I suppose, shadowing a variable before checking it
foo, bar := myFunc()
_, bar = myFunc()
fmt.Println(foo, bar)
Which is more of a general grievance of quite some people, but I don't think that has much to do with multiple returns - and you definitely have to look at the error to pave over it afterwards.
edit: https://goplay.tools/snippet/E69xFuIcG7I https://goplay.tools/snippet/E69xFuIcG7I
- brabel 1y agoYou're right, Go errors if you do not use a value, so it's very hard to forget to check for the error. I might have been thinking about another language, consider my comment about Go retracted.