Validating the connection
The connections to the database in the driver implementation may be pooled, and it is possible that the connection pulled out of the pool is broken. This recipe will show how to verify if the connection is alive.
Getting ready
Verify if Go is properly installed by calling the go version
command in your Terminal. If the command fails, follow the Getting ready section in the first recipe of this chapter.
How to do it...
- Open the console and create the folder
chapter08/recipe02
. - Navigate to the directory.
- Create the
verify.go
file with the following content:
package main import ( "context" "database/sql" "fmt" "time" _ "github.com/lib/pq" ) func main() { connStr := "postgres://postgres:postgres@ localhost:5432/example?sslmode=disable" db, err := sql.Open("postgres", connStr) if err != nil { panic(err) } defer...