golang 文件是否存在_如何测试Go中是否存在文件或目录?

golang 文件是否存在

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

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

In Bash, the [ -e ] tests can test whether a file or a directory exist. What are the corresponding Go ways for the test?

在Bash中, [ -e ]测试可以测试文件或目录是否存在。 有哪些相应的Go测试方式?



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
    }
}

翻译自: https://www.systutorials.com/how-to-test-a-file-or-directory-exists-in-go/

golang 文件是否存在