O'Reilly logo

Go Standard Library Cookbook by Radomir Sohlich

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How to do it...

  1. Open the console and create the folder chapter08/recipe01.
  2. Navigate to the directory.
  3. Create the connect.go file with the following content:
       package main       import (         "database/sql"         "fmt"         _ "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 db.Close()         err = db.Ping()         if err != nil {           panic(err)         }         fmt.Println("Ping OK")       }
  1. Execute the code by go run connect.go.
  2. See the output:

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required