shad-go/lectures/07-sql/sqlx/structscan.go
2020-04-09 14:15:54 +03:00

32 lines
518 B
Go

package sqlx
import (
"context"
"log"
"github.com/jmoiron/sqlx"
)
func Example(ctx context.Context, db *sqlx.DB) {
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 value struct {
ID int `db:"id"`
Name string `db:"name"`
}
if err := sqlx.StructScan(rows, &value); err != nil {
log.Fatal(err)
}
log.Println(value)
}
if err = rows.Err(); err != nil {
log.Fatal(err)
}
}