golang 文件是否存在

How to test a path (a file or directory exists) in golang?

如何在golang中测试路径(文件或目录存在)?

[ -e ]
[ -e ]


You can use the `os.Stat()` and `os.IsNotExist()` standard libraries functions together to test whether a file or a directory exists in Go.

您可以将os.Stat()和os.IsNotExist()标准库函数一起使用,以测试Go中是否存在文件或目录。

if _, err := os.Stat("/tmp/aaaa"); err != nil {
    if os.IsNotExist(err) {
        // file does not exists
    } else {
        // file exists
    }
}

golang 文件是否存在