aboutsummaryrefslogtreecommitdiff
path: root/src/pkg/path/path.go
diff options
context:
space:
mode:
authorRob Pike <r@golang.org>2011-12-22 14:08:34 -0800
committerRob Pike <r@golang.org>2011-12-22 14:08:34 -0800
commitb6122b0a64449da93d1a2d457239ebb00fce6cbc (patch)
tree9ac96ddf29eababb4da761ff9f6c44d1809392bd /src/pkg/path/path.go
parentc0589a21c9ec9f075d27037a62c809a7b0db200d (diff)
downloadgo-b6122b0a64449da93d1a2d457239ebb00fce6cbc.tar.xz
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
Diffstat (limited to 'src/pkg/path/path.go')
-rw-r--r--src/pkg/path/path.go18
1 files changed, 18 insertions, 0 deletions
diff --git a/src/pkg/path/path.go b/src/pkg/path/path.go
index 235384667c..20d89c9ff0 100644
--- a/src/pkg/path/path.go
+++ b/src/pkg/path/path.go
@@ -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
+}