-
-
Save shalaby/7b1fd4fa9e755b620e4a52ff32279ee6 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