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/recipe03.
  2. Navigate to the directory.
  3. Create the statement.go file with the following content:
        package main        import (          "database/sql"          "fmt"          _ "github.com/lib/pq"        )        const sel = "SELECT * FROM post;"        const trunc = "TRUNCATE TABLE post;"        const ins = "INSERT INTO post(ID,TITLE,CONTENT)                     VALUES (1,'Title 1','Content 1'),                     (2,'Title 2','Content 2') "        func main() {          db := createConnection()          defer db.Close()          _, err := db.Exec(trunc)          if err != nil {            panic(err)          } fmt.Println("Table ...

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