Skip to content

Instantly share code, notes, and snippets.

@jamescarr
Last active June 11, 2017 12:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamescarr/d9058698b370d33662f140548a4be55a to your computer and use it in GitHub Desktop.
Save jamescarr/d9058698b370d33662f140548a4be55a to your computer and use it in GitHub Desktop.
package main
import (
"database/sql"
_ "github.com/lib/pq"
"log"
)
func failOnError(e error) {
if e != nil {
panic(e)
}
}
type User struct {
UserId int
Name string
Email string
}
func main() {
db, err := sql.Open("postgres", "user=test password=test dbname=test sslmode=disable")
failOnError(err)
db.QueryRow(`DELETE FROM users`)
// Insert
var userid int
err = db.QueryRow(`INSERT INTO users(name, email)
VALUES('james', 'james@example.com') RETURNING user_id`).Scan(&userid)
log.Println("James inserted with user_id", userid)
failOnError(err)
rows, err := db.Query(`SELECT user_id, name, email FROM users`)
defer rows.Close()
users := []User{}
for rows.Next() {
var user User
err = rows.Scan(&user.UserId, &user.Name, &user.Email)
failOnError(err)
users = append(users, user)
}
log.Println(users)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment