Closed vgarvardt closed 2 years ago
"github.com/jackc/pgtype"
"github.com/jackc/pgx/v5/pgxpool"
This is mixing pgx versions. github.com/jackc/pgtype
is now part of the same repo. Use github.com/jackc/pgx/v5/pgtype
.
Thank you! This one works
package main
import (
"context"
"fmt"
"os"
"github.com/jackc/pgx/v5/pgtype"
"github.com/jackc/pgx/v5/pgxpool"
)
func main() {
ctx := context.TODO()
pgxCfg, err := pgxpool.ParseConfig(os.Getenv("TEST_POSTGRES"))
if err != nil {
panic(fmt.Errorf("could not parse DB URL: %w", err))
}
pgxPool, err := pgxpool.ConnectConfig(ctx, pgxCfg)
if err != nil {
panic(fmt.Errorf("could not init pgx conn pool: %w", err))
}
if err := pgxPool.Ping(ctx); err != nil {
panic(fmt.Errorf("could not ping DB: %w", err))
}
var arr pgtype.Array[int64]
if err := pgxPool.QueryRow(ctx, `SELECT ARRAY[1, 2]::bigint[]`).Scan(&arr); err != nil {
panic(fmt.Errorf("could not read static array value: %w", err))
}
fmt.Println(arr.Elements)
}
$ TEST_POSTGRES=postgres://test:test@localhost:49856/test?sslmode=disable go run ./test.go
[1 2]
Can you pls help me understanding how to read array column using pgtype
This is the simple test code I'm trying to make work:
result:
the same query running in pg:
it starts with
{
, so I'm doing something wrong with scanning, but can not get what exactly.docker-compose.yml
I'm using for testing:Thank you.