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

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

generic

解决方案

  1. 95% 成功率
    Use sql.NullString: var ns sql.NullString; rows.Scan(&ns); if ns.Valid { use ns.String }
  2. 90% 成功率
    Use pointers: var s *string; rows.Scan(&s); if s != nil { use *s }

无效尝试

常见但无效的做法:

  1. 70% 失败

    May cause incorrect logic; NULL vs empty differ.

  2. 40% 失败

    May not be possible; data already has NULLs.