DROP function mytest();
CREATE OR REPLACE FUNCTION mytest()
RETURNS TABLE(name text, age int)
AS
$$
SELECT name, age FROM names
$$
LANGUAGE sql;
Most of the examples I've seen with a stored procedure return a single row with a single column and can be used with QueryRow. Here I am using a table as the output. This above returns 4 rows:
mytest
------------
(bob,12)
(fred,18)
(james,22)
(bill,27)
(4 rows)
In Go, what is the idiomatic way to deal with the tuples:
rows, err := db.Query("SELECT mytest()")
if err != nil {
panic(err)
}
defer rows.Close()
for rows.Next() {
var items string
if err = rows.Scan(&items); err != nil {
panic(err)
}
log.Println(items) // items is a string...now what?
}
if err = rows.Err(); err != nil {
panic(err)
}
// prints some tuples:
(bob,12)
(fred,18)
(james,22)
(bill,27)
Maybe there's a package for this but I haven't found it ;(