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;
  1. Open the console and create the folder chapter08/recipe04.
  2. Navigate to the directory.
  3. Create the prepared.go file with the following content:
        package main        import (          "database/sql"          "fmt"          _ "github.com/lib/pq"        )        const trunc = "TRUNCATE TABLE post;"        const ins = "INSERT INTO post(ID,TITLE,CONTENT)                     VALUES ($1,$2,$3)"        var testTable = []struct {          ID int          Title string          Content string        }{          {1, "Title One", "Content of title one"},          {2, "Title Two", "Content of title two"},          {3, "Title Three", "Content of title three"}, } ...

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