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

How to determine whether a file exists in golang

(*-*)浩
(*-*)浩 Original
2019-12-31 09:24:32 2396browse

How to determine whether a file 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 error returned is nil, it shows that the file or folder exists(Recommended learning:Go)

## This If the wrong type returned uses OS.ISNOTEXIST () If it is judged to be true, 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

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 }

The above is the detailed content of How to determine whether 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 modularize golang Next article:How to modularize golang