Created
October 21, 2016 21:15
-
-
Save satreix/4bbc41cc56f996aa3bdfe074cd16065c to your computer and use it in GitHub Desktop.
PG Golang
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
docker run --name some-postgres -e POSTGRES_PASSWORD=mysecretpassword -d postgres -p 5432:5432 |
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" | |
) | |
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