go runtime_error ai_generated true

sql: Rows are closed

ID: go/database-sql-rows-closed

Also available as: JSON · Markdown · 中文
80%Fix Rate
87%Confidence
0Evidence
2024-01-30First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.15 active
1.20 active

Root Cause

Attempting to call Next() or Scan() on a sql.Rows object after it has been closed, either explicitly or due to an error.

generic

中文

在 sql.Rows 对象已关闭后(显式关闭或由于错误)尝试调用 Next() 或 Scan()。

Workarounds

  1. 95% success
    Always check errors and call rows.Close() in defer: defer rows.Close(); for rows.Next() { ... }
  2. 90% success
    Check rows.Err() after iteration: if err := rows.Err(); err != nil { handleErr(err) }

Dead Ends

Common approaches that don't work:

  1. 90% fail

    Rows are already closed; further calls will panic or return stale data.

  2. 60% fail

    May cause data inconsistency and resource leaks if not handled properly.