user6638204 user6638204 - 7 days ago 6
SQL Question

How to scan SQL array to []int64 in Go?

I am scanning an array of int from Postgres DB and it is returning as

[]uint8
. I need them in
[]int64
, how can I convert them into
[]int64
or how can I return them from the DB as
[]int64
? In my query I am selecting using the Array function in Postgres:
Array(col1)
where col1 is serial.

The error I am getting is:

unsupported Scan, storing driver.Value type []uint8 into type []int64

Answer

If you're using github.com/lib/pq, just use Int64Array.

col1arr := []int64{}
arr := pq.Int64Array{}
err := rows.Scan(&arr)
// ...
col1arr = []int64(arr)