Add check.IsDir

pull/53/head
rwxrob 2 years ago
parent 193727dc0f
commit d73b3288ea
No known key found for this signature in database
GPG Key ID: 2B9111F33082AE77

@ -1,6 +1,9 @@
package check
import "reflect"
import (
"os"
"reflect"
)
// IsNil is a shortcut for reflect.ValueOf(foo).IsNil() and should only
// be used when foo == nil is in question, such as whenever the value of
@ -8,3 +11,13 @@ import "reflect"
// this check instead just to be sure to avoid surprise (and extremely
// odd) logic errors. Nil is not "nil" in Go.
func IsNil(i interface{}) bool { return reflect.ValueOf(i).IsNil() }
// IsDir returns true if the path points to a directory. False is
// returned in both cases where there is an error or the path is not
// a directory.
func IsDir(path string) bool {
if s, _ := os.Stat(path); s != nil {
return s.IsDir()
}
return false
}

@ -21,3 +21,11 @@ func ExampleIsNil() {
// true
// true
}
func ExampleIsDir() {
fmt.Println(check.IsDir("testdata"))
fmt.Println(check.IsDir("nothing"))
// Output:
// true
// false
}

Loading…
Cancel
Save