Postgres SELECT in Go returns all columns as a row (using pq and database / sql)

I make a choice in Go using database / sql and pq Postgres driver:

rows, err := db.Query("SELECT (name, age) FROM people WHERE id = 1")

I tried to restore the values ​​in the usual way:

rows.Next()
name := ""
age := 0
err = rows.Scan(&name, &age)

but I got an error:

sql: expected 1 destination arguments in Scan, not 2

Documentation for sql. (* Rows) .Scan says that you can pass a pointer to a piece of byte, and it will be filled with raw Results. So I did this:

b := make([]byte, 1024*1024)
rows.Scan(&b)
fmt.Println(string(b))

who succeeded, seal:

(John,18)

, sql. (* Rows).Scan, , , , ( ). - pq, , . ?

+4
2

a_horse_with_no_name ! ... ?

Postgres SELECT (a, b) , . : Select a, b.

+4

function out, - , . :

var foo, bar string
err := db.QueryRow("select * from my_function()").Scan(&foo, &bar)

function ​​:

create or replace function my_function(
  out first_out varchar,
  out second_out json
) as $$
  -- etc.
$$ language plpgsql;
0

All Articles