Answers for "golang check path exists"

Go
0

check if directory exists in go

// exists returns whether the given file or directory exists
func exists(path string) (bool, error) {
    _, err := os.Stat(path)
    if err == nil { return true, nil }
    if os.IsNotExist(err) { return false, nil }
    return false, err
}
Posted by: Guest on September-17-2021
0

golang check if a path contains a valid directory

dir, err := os.Stat(dirName)
if err != nil {
	return filesHash, fmt.Errorf("failed to open directory, error: %w", err)
}
if !dir.IsDir() {
	return filesHash, fmt.Errorf("%q is not a directory", dir.Name())
}
Posted by: Guest on October-21-2020

Browse Popular Code Answers by Language