result, err := computeVector(input)
if err != nil {
return nil, fmt.Errorf("error computing vector: %w", err)
}
But that's a bit unwieldy to write in an article or talk. result, err := computeVector(input)
if err != nil {
return nil, fmt.Errorf("error computing vector: %w", err)
}
But that's a bit unwieldy to write in an article or talk.This statement is categorically false, and I would encourage you to search GitHub for `return err` under the Go language.
There are 200k+ code results, and a quick survey of the first few pages shows plain ol' `return err` to be _quite_ common.
Error handling would look something like this:
for err := callService(); err != nil {
logger.Info("Retrying...")
}
Or value, err := readValueFromFile("abc.conf")
if err != nil {
value = getDefault()
}
etc.Edit: you also deleted the context of your call. You didn't create an error type for Unwrap to match on. That means that downstream code only has extremely primitive errors to match on, with the context of the call gone. Just because you can track down the EOF error, doesn't mean that code calling this can do anything useful with that. 'Socket closed unexpectedly' is way less useful than 'Unable to update user record'.
But they were right about it losing error context. Basically errorf can only help you bubble up the lowest level error in a machine readable way. You have to write your own error type if you want to let code handle the higher level errors.