Hacker News new | past | comments | ask | show | jobs | submit login

What can you do if you detect an error in a "defer" clause? Besides panic?



With 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.


Well, 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.


One thing you can do is simply use BOTH a defer Close and a straight-path Close with error-checking.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: