Skip to content

Commit 340ac9b

Browse files
committed
Add Go Modules
1 parent f3d382d commit 340ac9b

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

go.mod

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module crud-golang-postgres-rest-apis
2+
3+
go 1.14
4+
5+
require github.com/lib/pq v1.7.0

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
github.com/lib/pq v1.7.0 h1:h93mCPfUSkaul3Ka/VG8uZdmW1uMHDGxzu0NWHuJmHY=
2+
github.com/lib/pq v1.7.0/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=

server.go

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package main
2+
3+
import (
4+
"database/sql"
5+
"fmt"
6+
7+
_ "github.com/lib/pq"
8+
)
9+
10+
const (
11+
DB_USER = "postgres"
12+
DB_PASSWORD = "Postgres"
13+
DB_NAME = "todo_db"
14+
)
15+
16+
func main() {
17+
dbinfo := fmt.Sprintf("user=%s password=%s dbname=%s sslmode=disable",
18+
DB_USER, DB_PASSWORD, DB_NAME)
19+
db, err := sql.Open("postgres", dbinfo)
20+
checkErr(err)
21+
defer db.Close()
22+
23+
fmt.Println("# Inserting values")
24+
25+
sqlStatement := `INSERT INTO task VALUES(155452,'dffdf44444d','as44taxie');`
26+
_, err = db.Exec(sqlStatement)
27+
checkErr(err)
28+
}
29+
30+
func checkErr(err error) {
31+
if err != nil {
32+
panic(err)
33+
}
34+
}

0 commit comments

Comments
 (0)