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. Run the following SQL script against your sample database:
        DROP TABLE IF EXISTS post;        CREATE TABLE post (          ID serial,          TITLE varchar(40),          CONTENT varchar(255),          CONSTRAINT pk_post PRIMARY KEY(ID)        );        SELECT * FROM post;        INSERT INTO post(ID,TITLE,CONTENT) VALUES                         (1,NULL,'Content One'),                        (2,'Title Two','Content Two');
  1. Open the console and create the folder chapter08/recipe08.
  2. Navigate to the directory.
  3. Create the querymap.go file with the following content:
        package main        import (          "database/sql"          "fmt"          _ "github.com/lib/pq"        )        const selOne = "SELECT id,title,content FROM post                        WHERE ID = $1;"        func main() {          db := createConnection()          defer db.Close()          rows, err := db.Query(selOne, 1)          if err != nil {            panic(err)          } cols, _ := rows.Columns() ...

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