3 ms·
More specifically, the `defer` keyword lets you handle cleanup in the case of failure a bit more neatly. The gotos can be replaced with stuff like this: f
by wonderzombie 14y ago
More specifically, the `defer` keyword lets you handle cleanup in the case of failure a bit more neatly.
The gotos can be replaced with stuff like this:
foo, err := bar()
if err != nil {
return err
}
defer foo.cleanup() // or what have you
Now, right before the function returns, Go will call `foo.cleanup()` and your `cleanup()` call sits right next to the initialization code. It's a lot easier to tell whether or not you are (or someone else is) doing cleanup properly.