package main
import (
"database/sql"
"fmt"
"log"
uuid "github.com/satori/go.uuid"
_ "github.com/lib/pq"
)
const (
host = "192.168.200.22"
port = 5432
user = "******"
password = "******"
dbname = "restapi_dev"
)
func main() {
psqlInfo := fmt.Sprintf(
"host=%s port=%d user=%s password=%s dbname=%s sslmode=disable",
host, port, user, password, dbname,
)
log.Println("Connecting", fmt.Sprintf("%s...", psqlInfo[:19]))
db, err := sql.Open("postgres", psqlInfo)
if err != nil {
panic(err)
}
log.Println("Sending ping...")
err = db.Ping()
if err != nil {
panic(err)
}
log.Println("Ok")
defer func() {
log.Println("Closing connection")
db.Close()
}()
log.Println("Successfuly connected!")
query := `select ref, title from todos;`
log.Println("Fetching data...")
rows, err := db.Query(query)
defer rows.Close()
var (
ref uuid.UUID
title string
)
log.Println("query result")
for rows.Next() {
if err := rows.Scan(&ref, &title); err != nil {
log.Fatal(err)
}
log.Println(fmt.Sprintf("%s %s", ref, title))
}
}
go mod init dsite.kz/test/check
Инициализация проекта
go get github.com/lib/pq
Получить либу postgres
go get github.com/satori/go.uuid
соответственно предыдущему