go
data_error
ai_generated
true
sql: Scan error on column index 3: converting NULL to string is unsupported
ID: go/database-scan-null-field
90%Fix Rate
88%Confidence
1Evidence
2023-03-10First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| Go 1.18 | active | — | — | — |
| Go 1.19 | active | — | — | — |
| Go 1.20 | active | — | — | — |
| Go 1.21 | active | — | — | — |
| Go 1.22 | active | — | — | — |
Root Cause
Scanning a NULL database value into a non-pointer Go type (e.g., string instead of *string or sql.NullString).
generic中文
将数据库中的 NULL 值扫描到非指针的 Go 类型(例如,使用 string 而不是 *string 或 sql.NullString)。
Official Documentation
https://pkg.go.dev/database/sql#NullStringWorkarounds
-
95% success Use sql.NullString or *string for nullable columns, then check .Valid before use
Use sql.NullString or *string for nullable columns, then check .Valid before use
-
90% success Define custom struct fields as pointers to handle NULLs
Define custom struct fields as pointers to handle NULLs
中文步骤
对可空列使用 sql.NullString 或 *string,并在使用前检查 .Valid
将自定义结构体字段定义为指针以处理 NULL
Dead Ends
Common approaches that don't work:
-
60% fail
This masks the data issue and can lead to incorrect business logic if NULL has semantic meaning.
-
80% fail
Pollutes the data and may break other queries or reports.