go
io_error
ai_generated
true
文件已关闭
file already closed
ID: go/os-file-already-closed
80%修复率
87%置信度
0证据数
2024-02-14首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 1.13 | active | — | — | — |
| 1.14 | active | — | — | — |
| 1.15 | active | — | — | — |
根因分析
尝试对已关闭的文件进行读、写或关闭操作,通常是由于双重关闭或在 defer Close() 后使用文件。
English
Attempting to read, write, or close a file that has already been closed, often due to double-close or using a file after defer Close().
解决方案
-
95% 成功率
Use a sync.Once to ensure Close() is called only once.
-
90% 成功率
Set the file variable to nil after Close() and check for nil before using.
无效尝试
常见但无效的做法:
-
70% 失败
The file pointer is not nil after closing; it's the underlying descriptor that is invalid.
-
60% 失败
Sync() does not prevent the file from being closed.