go io_error ai_generated true

error: open /tmp/foo.txt: no such file or directory

ID: go/os-file-not-exist-create

Also available as: JSON · Markdown · 中文
80%Fix Rate
86%Confidence
0Evidence
2024-03-22First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.20 active
1.21 active

Root Cause

尝试打开不存在的文件,但未使用创建标志。

generic

中文

尝试打开不存在的文件,但未使用创建标志。

Workarounds

  1. 95% success
    f, err := os.OpenFile("/tmp/foo.txt", os.O_RDWR|os.O_CREATE, 0644)
  2. 90% success
    先检查文件是否存在,再决定打开或创建。

Dead Ends

Common approaches that don't work:

  1. 100% fail

    文件不存在,后续操作会失败。

  2. 60% fail

    os.Create会截断现有文件,可能覆盖数据。