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

How to determine whether a directory is in golang

Release: 2020-01-13 09:56:58
Original
6755 people have browsed it

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()
}
Copy after login

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!

Related labels:
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
Latest Issues
How to choose golang web mvc framework
From 1970-01-01 08:00:00
0
0
0
Is it necessary to use nginx when using golang?
From 1970-01-01 08:00:00
0
0
0
golang - vim plug-in to write go
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template