-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
73 lines (56 loc) · 1.67 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"
_ "github.com/lib/pq"
)
// menambah tag sql pada struct Employee
type Employees struct {
ID int `sql:"id"`
Name string `sql:"name"`
Age int `sql:"age"`
Address string `sql:"address"`
Salary int `sql:"salary"`
}
func Connect() (*sql.DB, error) {
dns := "host=localhost user=postgres password=123 dbname=test_db_camp port=5435 sslmode=disable"
db, err := sql.Open("postgres", dns)
if err != nil {
return nil, err
}
return db, nil
}
func main() {
// buat koneksi ke database menggunakan func `Connect`
db, err := Connect()
if err != nil {
panic(err)
}
defer db.Close()
// create table employee
_, err = db.Exec(`CREATE TABLE IF NOT EXISTS employees (
id SERIAL PRIMARY KEY,
name VARCHAR(50),
age INT,
address VARCHAR(255),
salary INT
);`)
// insert data employee
_, err = db.Exec(`INSERT INTO employees (name, age, address, salary) VALUES ('John Doe', 30, 'New York', 2000);`)
// melakukan query untuk mendapatkan semua data dari tabel employee
row := db.QueryRow("SELECT * FROM employees WHERE id = 1")
// membuat struct baru untuk menampung data dari tabel employee
var listEmployee []Employees
// melakukan looping untuk menampung data dari rows ke struct Employee
var employees Employees
// kita tampung setiap baris data ke struct Employee
err = row.Scan(&employees.ID, &employees.Name, &employees.Age, &employees.Address, &employees.Salary)
if err != nil {
panic(err)
}
// kemudian kita tambahkan struct Employee ke listEmployee
listEmployee = append(listEmployee, employees)
fmt.Println("Data Employee")
// menampilkan data dari listEmployee
fmt.Println(listEmployee)
}