Golang determines whether the file exists:
// 判断所给路径文件/文件夹是否存在 func Exists(path string) bool { _, err := os.Stat(path) //os.Stat获取文件信息 if err != nil { if os.IsExist(err) { return true } return false } return true } // 判断所给路径是否为文件夹 func IsDir(path string) bool { s, err := os.Stat(path) if err != nil { return false } return s.IsDir() } // 判断所给路径是否为文件 func IsFile(path string) bool { return !IsDir(path) }
os.Stat(): can be used to obtain file information.
IsExist(): Determine whether the file or directory exists based on the error.
For more golang knowledge, please pay attention to the golang tutorial column.
The above is the detailed content of How to find whether a file exists in golang. For more information, please follow other related articles on the PHP Chinese website!