How to determine whether a directory exists in golang

angryTom
Release: 2020-02-15 09:18:58
Original
6569 people have browsed it

How to determine whether a directory exists in golang

How does golang determine whether a directory exists?

The method for golang to determine whether a file or folder exists is to use os.Stat() The error value returned by the function is judged:

If the error returned is nil, it means that the file or folder exists

If the error type returned is os.IsNotExist()Judged to be true, indicating that the file or folder does not exist

If the error returned is of other types, it is not sure whether it exists

func PathExists(path string) (bool, error) {
    _, err := os.Stat(path)
    if err == nil {
        return true, nil
    }
    if os.IsNotExist(err) {
        return false, nil
    }
    return false, err
}
Copy after login

For more golang knowledge, please pay attention to the golang tutorial column on the PHP Chinese website.

The above is the detailed content of How to determine whether a directory exists in golang. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!