-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
73 lines (55 loc) · 1.1 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package main
import (
"database/sql"
"fmt"
"go-echo-api/routes"
_ "github.com/lib/pq"
)
const (
host1 = "localhost"
port1 = 5431
user1 = "postgres"
password1 = "html5123"
dbname1 = "kepegawaian"
)
type pegawai struct {
id int
nama string
alamat string
telepon string
}
func main() {
e := routes.Init()
psqlInfo := fmt.Sprintf("host=%s port=%d user=%s "+
"password=%s dbname=%s sslmode=disable",
host1, port1, user1, password1, dbname1)
db, err := sql.Open("postgres", psqlInfo)
if err != nil {
panic(err)
}
defer db.Close()
err = db.Ping()
if err != nil {
panic(err)
}
rows, err := db.Query("SELECT * FROM pegawai ORDER BY id ASC")
defer rows.Close()
var result []pegawai
for rows.Next() {
var each = pegawai{}
var err = rows.Scan(&each.id, &each.nama, &each.alamat, &each.telepon)
if err != nil {
fmt.Println(err.Error())
return
}
result = append(result, each)
}
if err = rows.Err(); err != nil {
fmt.Println(err.Error())
return
}
for _, each := range result {
fmt.Println(each.nama)
}
e.Logger.Fatal(e.Start(":2020"))
}