github.com/DQNEO/babygo@v0.0.3/lib/path/path.go (about) 1 package path 2 3 import "github.com/DQNEO/babygo/lib/strings" 4 5 // "foo/bar/buz" => "foo/bar" 6 func Dir(path string) string { 7 if len(path) == 0 { 8 return "." 9 } 10 11 if path == "/" { 12 return "/" 13 } 14 15 found := strings.LastIndexByte(path, '/') 16 if found == -1 { 17 // not found 18 return path 19 } 20 21 return path[:found] 22 } 23 24 // "foo/bar/buz" => "buz" 25 func Base(path string) string { 26 if len(path) == 0 { 27 return "." 28 } 29 30 if path == "/" { 31 return "/" 32 } 33 34 if path[len(path)-1] == '/' { 35 path = path[0 : len(path)-1] 36 } 37 38 found := strings.LastIndexByte(path, '/') 39 if found == -1 { 40 // not found 41 return path 42 } 43 44 return path[found+1:] 45 }