Skip to content

Instantly share code, notes, and snippets.

@mwmahlberg
Created January 22, 2016 19:55
Show Gist options
  • Save mwmahlberg/24807f2a900860693668 to your computer and use it in GitHub Desktop.
Save mwmahlberg/24807f2a900860693668 to your computer and use it in GitHub Desktop.
package main
import (
"os"
"io/ioutil"
)
// FileReadable returns true only if path is readable.
func FileReadable(path string) bool {
fi, err := os.Stat(path)
if err != nil {
// We can not call stats, so it is definetly not readable
return false
}
if fi.Mode().IsRegular() {
// Open in RDONLY mode...
f, err := os.Open(path)
if err != nil {
// ...didn't work
return false
}
f.Close()
return true
} else if fi.Mode().IsDir() {
if _, err := ioutil.ReadDir(path); err != nil {
// Reading content of directory did not work
return false
}
return true
}
return false
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment