From 6ef0c341f948b704c0dd9f5ef06c4bb77d186c63 Mon Sep 17 00:00:00 2001 From: Joshua Shields Date: Wed, 15 Jan 2020 15:18:07 -0600 Subject: [PATCH] Update retrieving.md Make the warning about runtime panic closing rows more explicit. --- retrieving.md | 1 + 1 file changed, 1 insertion(+) diff --git a/retrieving.md b/retrieving.md index 33cee55..7883432 100644 --- a/retrieving.md +++ b/retrieving.md @@ -76,6 +76,7 @@ A couple parts of this are easy to get wrong, and can have bad consequences. * `rows.Close()` is a harmless no-op if it's already closed, so you can call it multiple times. Notice, however, that we check the error first, and only call `rows.Close()` if there isn't an error, in order to avoid a runtime panic. + For example, if `rows` is `nil`, `defer rows.Close()` will cause a nil pointer dereference panic after the function exits. * You should **always `defer rows.Close()`**, even if you also call `rows.Close()` explicitly at the end of the loop, which isn't a bad idea. * Don't `defer` within a loop. A deferred statement doesn't get executed until