Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

452
Vistas
How do I get a slice from a Postgres array in Golang?

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.

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

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]
over 4 years ago · Santiago Trujillo Denunciar

0

I don't want to use a additional driver (github.com/lib/pq), and I did not found any pgx way to do it other than creating my own type.

So I did one:

type Tags []string
func (t *Tags) Scan(v interface{}) error {
        if v == nil {
                *t = Tags{}
                return nil
        }
        s, ok := v.(string)
        if !ok {
                return fmt.Errorf("Scan is expected to receive a string from database, but got [%+v]", v)
        }
        s = strings.TrimPrefix(s, "{")
        s = strings.TrimSuffix(s, "}")
        *t = strings.Split(s, ",")
        return nil
}
func (t *Tags) Value() (driver.Value, error) {
        s := fmt.Sprintf("{%v}", strings.Join(([]string)(*t), ","))
        return s, nil
}

then you can scan the row returned from database like:

...
    err := rows.Scan(
        ...
        &Tags,
        ...
    )

and you can use it directly you your Exec queries.

This code works well with constants in arrays, but you need more work if want to use it in commas and brackets.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda