Golang database/sql.Rows.Close function usage examples

package database/sql

Close closes the Rows, preventing further enumeration. If Next returns false, the Rows are closed automatically and it will suffice to check the result of Err. Close is idempotent and does not affect the result of Err.

Golang database/sql.Rows.Close function usage examples

Example 1:

 rows, err := db.Query("select id, name from users where id = ?", 1)
 if err != nil {
  log.Fatal(err)
 }
 defer rows.Close()
 for rows.Next() {
  err := rows.Scan(&id, &name)
  if err != nil {
 log.Fatal(err)
  }
  log.Println(id, name)
 }

Example 2:

 microsecsSupported := false
 if rows, err := dbt.db.Query(`SELECT cast("00:00:00.1" as TIME(1)) = "00:00:00.1"`); err == nil {
 rows.Scan(&microsecsSupported)
 rows.Close()
 }

Reference :

http://golang.org/pkg/database/sql/#Rows.Close

Advertisement