diff --git a/pgtype/pgtype.go b/pgtype/pgtype.go index f8ad2bf3..3df322df 100644 --- a/pgtype/pgtype.go +++ b/pgtype/pgtype.go @@ -1921,13 +1921,15 @@ func (w *sqlScannerWrapper) Scan(src any) error { } var bufSrc []byte - switch src := src.(type) { - case string: - bufSrc = []byte(src) - case []byte: - bufSrc = src - default: - bufSrc = []byte(fmt.Sprint(bufSrc)) + if src != nil { + switch src := src.(type) { + case string: + bufSrc = []byte(src) + case []byte: + bufSrc = src + default: + bufSrc = []byte(fmt.Sprint(bufSrc)) + } } return w.m.Scan(t.OID, TextFormatCode, bufSrc, w.v) diff --git a/stdlib/sql_test.go b/stdlib/sql_test.go index ca2dccf3..18591d61 100644 --- a/stdlib/sql_test.go +++ b/stdlib/sql_test.go @@ -394,6 +394,10 @@ func TestConnQueryScanArray(t *testing.T) { err := db.QueryRow("select '{1,2,3}'::bigint[]").Scan(m.SQLScanner(&a)) require.NoError(t, err) assert.Equal(t, pgtype.Array[int64]{Elements: []int64{1, 2, 3}, Dims: []pgtype.ArrayDimension{{Length: 3, LowerBound: 1}}, Valid: true}, a) + + err = db.QueryRow("select null::bigint[]").Scan(m.SQLScanner(&a)) + require.NoError(t, err) + assert.Equal(t, pgtype.Array[int64]{Elements: nil, Dims: nil, Valid: false}, a) }) }