go
data_error
ai_generated
true
sql: 扫描第 0 列错误:不支持将 NULL 转换为 string
sql: Scan error on column index 0: converting NULL to string is unsupported
ID: go/sql-scan-into-nil-pointer
80%修复率
87%置信度
0证据数
2025-03-25首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 1.21 | active | — | — | — |
根因分析
数据库为某个列返回 NULL,但扫描到了不可为空的 Go 类型。
English
Database returned NULL for a column scanned into a non-nullable Go type.
解决方案
-
95% 成功率
Use sql.NullString: var ns sql.NullString; rows.Scan(&ns); if ns.Valid { use ns.String } -
90% 成功率
Use pointers: var s *string; rows.Scan(&s); if s != nil { use *s }
无效尝试
常见但无效的做法:
-
70% 失败
May cause incorrect logic; NULL vs empty differ.
-
40% 失败
May not be possible; data already has NULLs.