go
data_error
ai_generated
true
sql:第2列扫描错误:不支持将NULL转换为字符串
sql: Scan error on column index 2: converting NULL to string is unsupported
ID: go/database-sql-null-value
80%修复率
90%置信度
0证据数
2024-02-14首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 1.18 | active | — | — | — |
| 1.19 | active | — | — | — |
根因分析
将数据库中的NULL列扫描到不可为空的Go类型(如string)中,而未使用sql.NullString。
English
Scanning a NULL database column into a non-nullable Go type (e.g., string) without using sql.NullString.
解决方案
-
95% 成功率
var s sql.NullString if err := rows.Scan(&s); err != nil { // handle error } if s.Valid { fmt.Println(s.String) } -
80% 成功率
Use COALESCE in SQL query to convert NULL to default value.
无效尝试
常见但无效的做法:
-
70% 失败
NULL is not the same as an empty string and can lead to data loss.
-
90% 失败
Go does not allow direct casting of NULL to string.