Skip to content

Instantly share code, notes, and snippets.

@shalaby
Forked from agis/main.go
Created June 3, 2017 11:21
Show Gist options
  • Select an option

  • Save shalaby/7b1fd4fa9e755b620e4a52ff32279ee6 to your computer and use it in GitHub Desktop.

Select an option

Save shalaby/7b1fd4fa9e755b620e4a52ff32279ee6 to your computer and use it in GitHub Desktop.
Get SQL count in Postgres with Go (golang)
package main
import (
"database/sql"
"fmt"
"log"
_ "github.com/lib/pq"
)
func main() {
var count int
db, err := sql.Open("postgres", "user=test password=test dbname=foo sslmode=disable")
if err != nil {
log.Fatal(err)
}
defer db.Close()
row := db.QueryRow("SELECT COUNT(*) FROM table_name")
err := row.Scan(&count)
if err != nil {
log.Fatal(err)
}
fmt.Println(count)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment