Home > Backend Development > Golang > How to read whether a file exists in golang

How to read whether a file exists in golang

angryTom
Release: 2020-03-14 17:40:01
Original
1598 people have browsed it

How to read whether a file exists in golang

How does golang read whether a file exists?

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

Recommended: navicat tutorial

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

2. If the error type returned is true using os.IsNotExist(), it means the file or folder does not exist.

3. If the error type returned is of other types, it is not sure whether it exists. There are

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 related tutorials, please pay attention to the docker tutorial column on the PHP Chinese website.

The above is the detailed content of How to read whether a file 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 admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template