-
Notifications
You must be signed in to change notification settings - Fork 8
/
example_test.go
72 lines (58 loc) · 1.09 KB
/
example_test.go
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
package crud
import (
"time"
)
func ExampleInsert() {
db, er := createDb()
if er != nil {
return
}
defer db.Close()
/* Foo is defined elsewhere and has appropriately-tagged fields */
f := Foo{
Num: 42,
Str: "etc",
Time: time.Now(),
}
/* "foo" is the SQL table name, "foo_id" is the primary key for this type */
f.Id, er = Insert(db, "foo", "foo_id", f)
if er != nil {
/* Handle the error */
}
}
func ExampleUpdate() {
db, er := createDb()
if er != nil {
return
}
defer db.Close()
/* f is a Foo instance obtained from somewhere and modified */
f := Foo{
Id: 4,
Num: 49,
}
/* "foo" is the SQL table name, "foo_id" is the primary key for this type */
if er := Update(db, "foo", "foo_id", f) ; er != nil {
/* Handle the error */
}
}
func ExampleScan() {
db, er := createDb()
if er != nil {
return
}
defer db.Close()
rows, er := db.Query("SELECT * FROM foo")
if er != nil {
/* Handle error */
}
defer rows.Close()
fs := []Foo{}
for rows.Next() {
var f Foo
if er := Scan(rows, &f) ; er != nil {
/* Handle error */
}
fs = append(fs, f)
}
}