Golang determines whether it is a file or a folder

Release: 2020-03-27 11:57:05
Original
10187 people have browsed it

Golang determines whether it is a file or a folder

Go language method to determine whether it is a file or a folder:

// 判断所给路径文件/文件夹是否存在 
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)  
}
Copy after login

isdir:

Function: Determine input (string) Whether to represent a folder. This function can also be used to determine whether a folder exists.

Syntax format:

tf = isdir('A')

If A is a folder, return logical 1 (true), otherwise return 0 (false).

For more golang knowledge, please pay attention to the golang tutorial column on the PHP Chinese website.

The above is the detailed content of Golang determines whether it is a file or a folder. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!