3 ms·
They admit it's contrived, but this isn't very convincing func printSum(a, b string) error { x, err := strconv.Atoi(a) if err != nil {
by ziml77 1y ago
They admit it's contrived, but this isn't very convincing
func printSum(a, b string) error {
x, err := strconv.Atoi(a)
if err != nil {
return fmt.Errorf("invalid integer: %q", a)
}
y, err := strconv.Atoi(b)
if err != nil {
return fmt.Errorf("invalid integer: %q", b)
}
fmt.Println("result:", x + y)
return nil
}
It's not adding anything that the Atoi function couldn't have reported. That's a perfect case for blindly passing an error up the stack.
- nasretdinov 1y agoIt does, it says which one of the two integers was incorrect
- Zambyte 1y agoWhy can't Atoi report that?
- nasretdinov 1y agoI think Atoi actually does say that, but it's just a toy example. Most often functions outside the standard library don't contain the arguments in their error values