Skip to content

Commit 50ca1a5

Browse files
committed
database/sql: add "defer rows.Close()" to the example code.
Strictly speaking, it's not necessary in example_test.go, as the Rows.Close docs say that "If Next returns false, the Rows are closed automatically". However, if the for loop breaks or returns early, it's not obvious that you'll leak unless you explicitly call Rows.Close. LGTM=bradfitz R=bradfitz CC=golang-codereviews, rsc https://golang.org/cl/79330043
1 parent 3750904 commit 50ca1a5

File tree

2 files changed

+2
-0
lines changed

2 files changed

+2
-0
lines changed

src/pkg/database/sql/example_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ func ExampleDB_Query() {
1818
if err != nil {
1919
log.Fatal(err)
2020
}
21+
defer rows.Close()
2122
for rows.Next() {
2223
var name string
2324
if err := rows.Scan(&name); err != nil {

src/pkg/database/sql/sql.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1494,6 +1494,7 @@ func (s *Stmt) finalClose() error {
14941494
//
14951495
// rows, err := db.Query("SELECT ...")
14961496
// ...
1497+
// defer rows.Close()
14971498
// for rows.Next() {
14981499
// var id int
14991500
// var name string

0 commit comments

Comments
 (0)