-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_array_test.go
49 lines (42 loc) · 1.05 KB
/
example_array_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
package pg_test
import (
"fmt"
"gopkg.in/pg.v5"
)
func ExampleDB_Model_postgresArrayStructTag() {
type Item struct {
Id int64
Emails []string `pg:",array"` // marshalled as PostgreSQL array
Numbers [][]int `pg:",array"` // marshalled as PostgreSQL array
}
_, err := db.Exec(`CREATE TEMP TABLE items (id serial, emails text[], numbers int[][])`)
if err != nil {
panic(err)
}
defer db.Exec("DROP TABLE items")
item1 := Item{
Id: 1,
Emails: []string{"[email protected]", "[email protected]"},
Numbers: [][]int{{1, 2}, {3, 4}},
}
if err := db.Insert(&item1); err != nil {
panic(err)
}
var item Item
err = db.Model(&item).Where("id = ?", 1).Select()
if err != nil {
panic(err)
}
fmt.Println(item)
// Output: {1 [[email protected] [email protected]] [[1 2] [3 4]]}
}
func ExampleArray() {
src := []string{"[email protected]", "[email protected]"}
var dst []string
_, err := db.QueryOne(pg.Scan(pg.Array(&dst)), `SELECT ?`, pg.Array(src))
if err != nil {
panic(err)
}
fmt.Println(dst)
// Output: [[email protected] [email protected]]
}