Skip to content

Instantly share code, notes, and snippets.

@satreix
Created October 21, 2016 21:15
Show Gist options
  • Save satreix/4bbc41cc56f996aa3bdfe074cd16065c to your computer and use it in GitHub Desktop.
Save satreix/4bbc41cc56f996aa3bdfe074cd16065c to your computer and use it in GitHub Desktop.
PG Golang
docker run --name some-postgres -e POSTGRES_PASSWORD=mysecretpassword -d postgres -p 5432:5432
package main
import (
"database/sql"
"fmt"
_ "github.com/lib/pq"
)
func main() {
db, err := sql.Open("postgres", "host=localhost user=postgres dbname=postgres sslmode=disable password='mysecretpassword'")
if err != nil {
panic(err)
}
rows, err := db.Query("SELECT 1;")
if err != nil {
panic(err)
}
defer rows.Close()
for rows.Next() {
var i int
err := rows.Scan(&i)
if err != nil {
panic(err)
}
fmt.Printf("- %#v\n", i)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment