3 ms·
What can you do if you detect an error in a "defer" clause? Besides panic?
by Animats 10y ago
What can you do if you detect an error in a "defer" clause? Besides panic?
- ansible 10y agoWell, I don't know if panic is the right option. However, checking the return value is always a good idea, and any errors should at least be logged. I too wish it was less cumbersome to use defer properly.
- giovannibajo1 10y agoOne thing you can do is simply use BOTH a defer Close and a straight-path Close with error-checking.
- amscanne 10y agoWith named return values, the defer can modify them. For example: func Foo() (err error) { defer func() { err = errors.New("some error") }() } The function will return the "some error" error.