Daniël de Kok
|| at Jun 27, 2015 at 9:56 am
On Sat, Jun 27, 2015, at 02:05, aihui zhu wrote:
Is conn.Close() or rows.Close() must be called in a deferred function
to avoid memory leak caused by panic?
There are three ways you can exit a function:
- An early return.
- You reach the end of a function.
- A panic occurs.
Without defer, you would have to cover every possible exit path:
- For every return, ensure that Close() is called.
- At the end of a function, ensure that Close() is called.
- Ensure that Close() is called in case of a panic.
defer gives you a way of saying: no matter how you exit this function,
call this when it happens. This is far cleaner and less error-prone than
covering every possible exit (and using recover requires defer anyway).
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to email@example.com.
For more options, visit https://groups.google.com/d/optout.