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,'Title One','Content One'),                       (2,NULL,'Content Two');
  1. Open the console and create the folder chapter08/recipe07.
  2. Navigate to the directory.
  3. Create the data.go file with the following content:
        package main        import (          "database/sql"          "fmt"          _ "github.com/lib/pq"        )        const sel = `SELECT title,content FROM post;        SELECT 1234 NUM; `         const selOne = "SELECT title,content FROM post                        WHERE ID = $1;"        type Post struct {          Name sql.NullString          Text sql.NullString        }        func main() { db := ...

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