shad-go/lectures/07-sql/query/query.go
2020-04-09 16:03:44 +03:00

29 lines
436 B
Go

package query
import (
"context"
"database/sql"
"log"
)
func Query(ctx context.Context, db *sql.DB) {
rows, err := db.QueryContext(ctx, "SELECT id, name FROM users")
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)
}
}