FAQ
Is conn.Close() or rows.Close() must be called in a deferred function
to avoid memory leak caused by panic?

Thanks.

--
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 golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Aihui zhu at Jun 27, 2015 at 8:14 am
    Is conn.Close() or rows.Close() must be called in a deferred function
    to avoid memory leak caused by panic?

    Thanks.

    --
    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 golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • 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).

    Take care,
    Daniël

    --
    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 golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedJun 27, '15 at 8:14a
activeJun 27, '15 at 9:56a
posts3
users2
websitegolang.org

2 users in discussion

Aihui zhu: 2 posts Daniël de Kok: 1 post

People

Translate

site design / logo © 2022 Grokbase