-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
59 lines (50 loc) · 1.04 KB
/
main.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
package main
import (
"log"
"time"
"github.com/go-pg/pg"
"github.com/go-pg/pg/orm"
"github.com/tommy-42/go-pg-reproducer/user"
)
var (
DB *pg.DB
)
func init() {
// Options to connect to PostgreSQL
option := &pg.Options{
User: "gaming",
Password: "game42",
Database: "game",
Addr: "localhost:5432",
RetryStatementTimeout: true,
MaxRetries: 4,
MinRetryBackoff: 250 * time.Millisecond,
}
// Connect to PostgresQL
DB = pg.Connect(option)
err := DB.CreateTable((*user.User)(nil), &orm.CreateTableOptions{
Temp: true, // create temp table
FKConstraints: true,
})
if err != nil {
log.Printf("Err CreateTable: %+v", err)
panic(err)
}
err = DB.Insert(&user.User{Id: "1", Username: "test"})
if err != nil {
log.Printf("Err Inserting user")
panic(err)
}
}
func main() {
var user *user.User
err := DB.Model(user).Where("username = ?", "test").Select()
if err != nil {
if err == pg.ErrNoRows {
log.Printf("ErrNoRows")
panic(err)
}
log.Printf("Err: %+v", err)
panic(err)
}
}