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

其他格式: JSON · Markdown 中文 · English
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.

generic

解决方案

  1. 92% 成功率
    var email sql.NullString
    err := row.Scan(&email)
    if email.Valid { u.Email = email.String }
  2. 88% 成功率
    Use a pointer *string for nullable columns.

无效尝试

常见但无效的做法:

  1. 60% 失败

    Loses the distinction between empty and NULL.

  2. 65% 失败

    Requires type assertion and is error-prone.