Created
April 28, 2017 10:40
-
-
Save agis/7e8cd4c7a20d037c01e663402fcad9d0 to your computer and use it in GitHub Desktop.
Get SQL count in Postgres with Go (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
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
Full example of querying the results with count by where clause with order and offset: