FileExists reports whether a path holds a regular file and DirExists whether it holds a directory, both answering false for anything they cannot look at. Comes with doc comments and table-driven tests. (closes #15) Model: opus-5
32 lines
843 B
Go
32 lines
843 B
Go
package util
|
|
|
|
import "os"
|
|
|
|
// FileExists reports whether there is a regular file at path. A directory, a
|
|
// device node or a socket gives false, and so does anything the process is not
|
|
// allowed to look at, since from the caller's point of view there is no usable
|
|
// file there either way.
|
|
//
|
|
// Symbolic links are followed, so a link pointing at a regular file gives true
|
|
// and a link pointing at nothing gives false.
|
|
func FileExists(path string) bool {
|
|
info, err := os.Stat(path)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
|
|
return info.Mode().IsRegular()
|
|
}
|
|
|
|
// DirExists reports whether there is a directory at path. As with FileExists,
|
|
// anything that cannot be looked at gives false, and symbolic links are
|
|
// followed.
|
|
func DirExists(path string) bool {
|
|
info, err := os.Stat(path)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
|
|
return info.IsDir()
|
|
}
|