Standard library / database/sql

sql.Rows.Scan()

Copies columns from the current row into matching destinations. Scan requires a preceding successful Next, and the destination count must match the columns. The example uses a fixed-command educational driver, not a real database. Expected stdout: 'Ada\n'.

Formal Syntax & Structure

go syntaxgo
func (rs *Rows) Scan(dest ...any) error

Parameters & Attributes

NameTypeDescription
dest...anyPointers or Scanner values receiving result columns.

Return Value

error

Copies columns from the current row into matching destinations. Check returned errors where applicable.

Working Example

Example Snippetgo
package main
import (  "context"  "database/sql"  "database/sql/driver"  "errors"  "fmt"  "io"  "reflect"  "strings")
// demoDriver recognizes a few fixed fixture commands; it is not a SQL engine.type demoDriver struct{}
func (demoDriver) Open(string) (driver.Conn, error) { return &demoConn{}, nil }
type demoConnector struct{}
func (demoConnector) Connect(ctx context.Context) (driver.Conn, error) {  if err := ctx.Err(); err != nil {    return nil, err  }  return &demoConn{}, nil}func (demoConnector) Driver() driver.Driver { return demoDriver{} }
type demoConn struct{}
func (*demoConn) Close() error                            { return nil }func (c *demoConn) Prepare(q string) (driver.Stmt, error) { return &demoStmt{c, q}, nil }func (*demoConn) Begin() (driver.Tx, error)               { return demoTx{}, nil }func (c *demoConn) BeginTx(ctx context.Context, o driver.TxOptions) (driver.Tx, error) {  if err := ctx.Err(); err != nil {    return nil, err  }  if o.Isolation != 0 || o.ReadOnly {    return nil, errors.New("unsupported options")  }  return c.Begin()}func (*demoConn) Ping(ctx context.Context) error { return ctx.Err() }func (*demoConn) ExecContext(ctx context.Context, q string, a []driver.NamedValue) (driver.Result, error) {  if err := ctx.Err(); err != nil {    return nil, err  }  if q != "UPDATE demo SET value = ?" || len(a) != 1 {    return nil, errors.New("fixture command or argument mismatch")  }  return driver.RowsAffected(1), nil}func (*demoConn) QueryContext(ctx context.Context, q string, a []driver.NamedValue) (driver.Rows, error) {  if err := ctx.Err(); err != nil {    return nil, err  }  switch q {  case "SELECT value FROM demo WHERE id = ?":    if len(a) != 1 {      return nil, errors.New("missing argument")    }    if a[0].Value == int64(1) {      return &demoRows{values: []driver.Value{"Ada"}}, nil    }    return &demoRows{}, nil  case "SELECT value FROM demo":    return &demoRows{values: []driver.Value{"Ada", nil}}, nil  default:    return nil, errors.New("unknown fixture query")  }}
type demoStmt struct {  c *demoConn  q string}
func (*demoStmt) Close() error { return nil }func (s *demoStmt) NumInput() int {  if strings.Contains(s.q, "?") {    return 1  }  return 0}func (s *demoStmt) Exec(a []driver.Value) (driver.Result, error) {  return s.c.ExecContext(context.Background(), s.q, named(a))}func (s *demoStmt) Query(a []driver.Value) (driver.Rows, error) {  return s.c.QueryContext(context.Background(), s.q, named(a))}func named(a []driver.Value) []driver.NamedValue {  r := make([]driver.NamedValue, len(a))  for i, v := range a {    r[i] = driver.NamedValue{Ordinal: i + 1, Value: v}  }  return r}
type demoTx struct{}
func (demoTx) Commit() error   { return nil }func (demoTx) Rollback() error { return nil }
type demoRows struct {  values []driver.Value  i      int}
func (*demoRows) Columns() []string { return []string{"value"} }func (*demoRows) Close() error      { return nil }func (r *demoRows) Next(dst []driver.Value) error {  if r.i >= len(r.values) {    return io.EOF  }  dst[0] = r.values[r.i]  r.i++  return nil}func (*demoRows) ColumnTypeDatabaseTypeName(int) string { return "VARCHAR" }func (*demoRows) ColumnTypeLength(int) (int64, bool)    { return 64, true }func (*demoRows) ColumnTypeNullable(int) (bool, bool)   { return true, true }func (*demoRows) ColumnTypeScanType(int) reflect.Type   { return reflect.TypeOf("") }func open() *sql.DB                                     { return sql.OpenDB(demoConnector{}) }func must[T any](v T, e error) T {  if e != nil {    panic(e)  }  return v}func check(e error) {  if e != nil {    panic(e)  }}
func main() {  db := open()  defer db.Close()  r := must(db.Query("SELECT value FROM demo"))  defer r.Close()  if !r.Next() {    panic("missing row")  }  var v string  check(r.Scan(&v))  fmt.Println(v)}
Go Reference