Home >Backend Development >Golang >How to determine whether a directory is in golang

How to determine whether a directory is in golang

尚
Original
2020-01-13 09:56:586560browse

How to determine whether a directory is in golang

How to determine whether it is a file or a directory in golang:

package main
import (
    "os"
        "fmt"
)
func main()  {
    file := "/root/data/testFile.txt"
        fmt.Printf("%s is file: %v\n", file, IsFile(file))
}
// IsFile checks whether the path is a file,
// it returns false when it's a directory or does not exist.
func IsFile(f string) bool {
    fi, e := os.Stat(f)
    if e != nil {
        return false
    }
    return !fi.IsDir()
}

os.path.isdir() is used to determine whether the object is a directory. Returns true if the specified file is a directory, false otherwise.

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

The above is the detailed content of How to determine whether a directory is 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