path: Dir

There was Base but not Dir, so fill in the gap.

R=golang-dev, rsc
CC=golang-dev
https://golang.org/cl/5504076
This commit is contained in:
Rob Pike 2011-12-22 14:08:34 -08:00
parent c0589a21c9
commit b6122b0a64
2 changed files with 49 additions and 8 deletions

View file

@ -160,3 +160,21 @@ func Base(path string) string {
func IsAbs(path string) bool {
return len(path) > 0 && path[0] == '/'
}
// Dir returns the all but the last element of path, typically the path's directory.
// Trailing path separators are removed before processing.
// If the path is empty, Dir returns ".".
// If the path consists entirely of separators, Dir returns a single separator.
// The returned path does not end in a separator unless it is the root directory.
func Dir(path string) string {
dir, _ := Split(path)
dir = Clean(dir)
last := len(dir) - 1
if last > 0 && dir[last] == '/' {
dir = dir[:last]
}
if dir == "" {
dir = "."
}
return dir
}