Home > Article > Backend Development > 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!