shad-go/lectures/07-sql/resources/rows.go

25 lines
369 B
Go
Raw Normal View History

2020-04-09 13:00:41 +00:00
package resources
import (
"context"
"database/sql"
"log"
)
func RowsExhaust(ctx context.Context, db *sql.DB) {
rows, err := db.QueryContext(ctx, "SELECT id, name FROM users")
if err != nil {
log.Fatal(err)
}
if rows.Next() {
var id int
var name string
if err := rows.Scan(&id, &name); err != nil {
log.Fatal(err)
}
log.Println(id, name)
}
}