go
data_error
ai_generated
true
sql:Scan 错误,列索引 2 名称 "email":不支持将 NULL 转换为 string
sql: Scan error on column index 2, name "email": converting NULL to string is unsupported
ID: go/sql-converting-null-to-string
80%修复率
89%置信度
0证据数
2024-03-04首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 1.18 | active | — | — | — |
| 1.21 | active | — | — | — |
根因分析
向非指针 string 扫描可空列。NULL 无法赋给 string。
English
A nullable column was scanned into a non-pointer string. NULL cannot be assigned to string.
解决方案
-
92% 成功率
var email sql.NullString err := row.Scan(&email) if email.Valid { u.Email = email.String } -
88% 成功率
Use a pointer *string for nullable columns.
无效尝试
常见但无效的做法:
-
60% 失败
Loses the distinction between empty and NULL.
-
65% 失败
Requires type assertion and is error-prone.