Golang database/sql.Row type and Scan function example

package database/sql

Row is the result of calling QueryRow to select a single row.

Scan copies the columns from the matched row into the values pointed at by dest. If more than one row matches the query, Scan uses the first row and discards the rest. If no row matches the query, Scan returns ErrNoRows.

Golang database/sql.Row type usage example

 var username string
 err = db.QueryRow("select username from accounts where id = ?", 1).Scan(&username)
 if err != nil {
  log.Fatal(err)
 }
 fmt.Println(name)

References :

http://golang.org/pkg/database/sql/#Row

http://golang.org/pkg/database/sql/#Row.Scan

Advertisement