Skip to content

Instantly share code, notes, and snippets.

@agis agis/main.go
Created Apr 28, 2017

Embed
What would you like to do?
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)
}
@shyandsy

This comment has been minimized.

Copy link

shyandsy commented Jun 14, 2019

what if the select statement has lots of condition?

@chagui

This comment has been minimized.

Copy link

chagui commented Oct 23, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.