3 ms·
You might like: _, err := somethingThatFails(arg) if err != nil { return fmt.Errorf("failed with arg=%v, %v", arg, err) } Now the use for this
by AYBABTME 13y ago
You might like:
_, err := somethingThatFails(arg)
if err != nil {
return fmt.Errorf("failed with arg=%v, %v", arg, err)
}
Now the use for this is not obvious from this toy example, but having a `printf`-like function that produces error is great to return errors with more context.
The difference from your example is that the info in `err` is not lost when passed up.
- sambeau 13y agoI do like that. (I just picked (and cleaned up) the first piece of simple error code that I thought would be easy to use.)
- benmanns 13y agoAnd you might like: if _, err := somethingThatFails(arg); err != nil { return fmt.Errorf("failed with arg=%v, %v", arg, err) }
- AYBABTME 13y agoIn that case I was ignoring the returned value for the sake of the example, otherwise you lose the declaration when it's in a `if`.