I have very little knowledge of Go. It seems like a pragmatic language with good tooling. And there exists a PostgreSQL driver written in pure Go that I can use.
The first step is to install Go and set up the environment. Once ready, the PostgreSQL driver can be installed in the workspace with the command go get github.com/lib/pq.
Then we create a project named github.com/danidiaz/hellopq in the workspace, with the following Go file:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main
import (
"database/sql"
"fmt"
_ "github.com/lib/pq"
"time"
)
func SetupDB() *sql.DB {
db, err := sql.Open("postgres",
"dbname=admin user=admin"+
" password=admin port=9996 sslmode=disable")
PanicIf(err)
return db
}
func PanicIf(err error) {
if err != nil {
panic(err)
}
}
func main() {
dbhandle := SetupDB()
rows, err := dbhandle.Query("select id, last_updated from release")
PanicIf(err)
defer rows.Close()
for rows.Next() {
var id int
var last_updated time.Time
err = rows.Scan(&id, &last_updated)
fmt.Printf("id %v last_updated %v\n", id, last_updated)
}
}
The code was adapted from the one shown in this video. The connection parameters were taken from here.Executing the program, it seems to work!
But I have a remaining doubt: how to properly handle the nullability of the last_updated column?