Home>Article>Backend Development> How to get whether a file in a directory exists in golang

How to get whether a file in a directory exists in golang

尚
Original
2020-03-11 17:30:13 1588browse

How to get whether a file in a directory exists in golang

The way Golang determines whether a file or folder exists is to use the error value returned by the os.Stat() function to determine:

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

If the returned error type is judged to be true using os.IsNotExist(), it indicates that the file or folder does not exist

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

Determine whether the given path file/folder 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 }

For more go language knowledge, please pay attention to the php Chinese websitego language tutorialColumn.

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

Statement:
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
Previous article:Why is Go so “fast”? Next article:Why is Go so “fast”?