go
io_error
ai_generated
true
error: open /tmp/foo.txt: no such file or directory
ID: go/os-file-not-exist-create
80%Fix Rate
86%Confidence
0Evidence
2024-03-22First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 1.20 | active | — | — | — |
| 1.21 | active | — | — | — |
Root Cause
尝试打开不存在的文件,但未使用创建标志。
generic中文
尝试打开不存在的文件,但未使用创建标志。
Workarounds
-
95% success
f, err := os.OpenFile("/tmp/foo.txt", os.O_RDWR|os.O_CREATE, 0644) -
90% success
先检查文件是否存在,再决定打开或创建。
Dead Ends
Common approaches that don't work:
-
100% fail
文件不存在,后续操作会失败。
-
60% fail
os.Create会截断现有文件,可能覆盖数据。