go
data_error
ai_generated
true
sql: Scan error on column index 0: converting NULL to string is unsupported
ID: go/sql-scan-into-nil-pointer
80%Fix Rate
87%Confidence
0Evidence
2025-03-25First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 1.21 | active | — | — | — |
Root Cause
Database returned NULL for a column scanned into a non-nullable Go type.
generic中文
数据库为某个列返回 NULL,但扫描到了不可为空的 Go 类型。
Workarounds
-
95% success
Use sql.NullString: var ns sql.NullString; rows.Scan(&ns); if ns.Valid { use ns.String } -
90% success
Use pointers: var s *string; rows.Scan(&s); if s != nil { use *s }
Dead Ends
Common approaches that don't work:
-
70% fail
May cause incorrect logic; NULL vs empty differ.
-
40% fail
May not be possible; data already has NULLs.