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:
        CREATE OR REPLACE FUNCTION format_name        (firstname Text,lastname Text,age INT) RETURNS         VARCHAR AS $$        BEGIN          RETURN trim(firstname) ||' '||trim(lastname) ||' ('||age||')';        END;        $$ LANGUAGE plpgsql;
  1. Open the console and create the folder chapter08/recipe10.
  2. Navigate to the directory.
  3. Create the procedure.go file with the following content:
        package main        import (          "database/sql"          "fmt"          _ "github.com/go-sql-driver/mysql"          _ "github.com/lib/pq"        )        const call = "select * from format_name($1,$2,$3)"        const callMySQL = "CALL simpleproc(?)"        type Result struct {          Name string          Category int        }        func main() {          db := createConnection()          defer db.Close()          r := Result{} if err := db.QueryRow(call, ...

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