如何从Golang的Postgres数组中获取切片?

Let's say I have a postgres query like:

SELECT
  id,
ARRAY_AGG(code) AS code
  FROM
    codes
WHERE id = '9252781' 
GROUP BY id;

My return looks like:

 id        |  codes
-----------+-------------
 9252781   | {H01,H02}

Both id and codes are varchar.

In Golang when I scan along the rows for my result, it just freezes. No error, nothing.

If you're using the github.com/lib/pq postgres driver you can use their pq.Array helper function to scan and store postgres arrays.

var id string
var arr []string

row := db.QueryRow(`SELECT '9252781', ARRAY['H01','H02']`)
if err := row.Scan(&id, pq.Array(&arr)); err != nil {
    log.Fatal(err)
}

log.Println(id, arr)
// 9252781 [H01 H02]