mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
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:
parent
c0589a21c9
commit
b6122b0a64
2 changed files with 49 additions and 8 deletions
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue