Home>Article>Backend Development> How to check if a file exists in golang

How to check if a file exists in golang

尚
Original
2019-12-26 15:54:28 2741browse

How to check if a file exists in golang

Go language detects whether the file exists. First create a FileInfo. If no error is reported, then check whether it is a directory through IsDir():

finfo, err := os.Stat("filename.txt") if err != nil { // no such file or dir return } if finfo.IsDir() { // it's a file } else { // it's a directory }

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

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

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

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

For more golang knowledge, please pay attention to thegolang tutorialcolumn.

The above is the detailed content of How to check if a file 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:How to compile golang Next article:How to compile golang