-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
77 lines (63 loc) · 1.3 KB
/
Copy pathexample_test.go
File metadata and controls
77 lines (63 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package sqlz_test
import (
"context"
"database/sql"
"fmt"
"log"
"time"
"github.com/semrekkers/sqlz"
)
var (
ctx context.Context
db *sql.DB
)
type User struct {
ID int
Name string
FirstName string `db:"first_name"`
LastName string `db:"last_name"`
Age int
DeletedAt *time.Time `db:"deleted_at"`
AppValue []byte `db:"-"`
}
func ExampleScan_struct() {
row, err := db.QueryContext(ctx, "SELECT * FROM users WHERE id = 123")
if err != nil {
log.Fatal(err)
}
defer row.Close()
var record User
if err = sqlz.Scan(ctx, row, &record); err != nil {
log.Fatal(err)
}
log.Println(record)
}
func ExampleScan_slice() {
rows, err := db.QueryContext(ctx, "SELECT * FROM users ORDER BY id LIMIT 10")
if err != nil {
log.Fatal(err)
}
defer rows.Close()
var records []*User
if err = sqlz.Scan(ctx, rows, &records); err != nil {
log.Fatal(err)
}
log.Println(records)
}
func ExampleScan_chan() {
records := make(chan *User, 8)
go func() {
defer close(records)
rows, err := db.QueryContext(ctx, "SELECT id FROM users WHERE deleted_at IS NULL ORDER BY id")
if err != nil {
log.Fatal(err)
}
defer rows.Close()
if err = sqlz.Scan(ctx, rows, records); err != nil {
log.Fatal(err)
}
}()
for user := range records {
fmt.Println("found active user:", user.ID)
}
}