Skip to content

How to scan an array that can be null? #1923

Answered by jackc
kalensk asked this question in Q&A
Discussion options

You must be logged in to vote

That should work using the native pgx v5 interface. However, since you are using database/sql / stdlib you will need to use pgtype.Map.SQLScanner().

Something like (untested, but I think it should work):

map := pgtype.NewMap() // this can be reused, but it is not concurrency safe

var output []string
err := sql.QueryRow(`select '{"foo", "NULL", " NULL "}'::name[];`).Scan(map.SQLScanner(&output))
if err != nil {
		return err
}

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@kalensk
Comment options

@jackc
Comment options

jackc Mar 2, 2024
Maintainer

Answer selected by kalensk
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants