shad-go/lectures/07-sql/query/query.go

20 lines
355 B
Go
Raw Normal View History

2020-04-08 19:20:23 +00:00
rows, err := db.QueryContext(ctx, "SELECT id, name FROM users WHERE id = $1", 1)
if err != nil {
log.Fatal(err)
}
defer rows.Close()
for rows.Next() {
var id int
var name string
if err := rows.Scan(&id, &name); err != nil {
log.Fatal(err)
}
log.Println(id, name)
}
if err = rows.Err(); err != nil {
log.Fatal(err)
}