github.com/0chain/gosdk@v1.17.11/core/pathutil/path.go (about) 1 // Provides utility functions for working with file paths. 2 package pathutil 3 4 import ( 5 "strings" 6 ) 7 8 // Split splits path immediately following the final Separator, 9 // separating it into a directory and file name component. 10 // If there is no Separator in path, Split returns an empty dir 11 // and file set to path. 12 // The returned values have the property that path = dir+file. 13 // - path is the path to be split. 14 func Split(path string) (dir, file string) { 15 if path == "" { 16 return "", "" 17 } 18 19 if path == "/" { 20 return "/", "" 21 } 22 23 i := strings.LastIndex(path, "/") 24 25 if i == -1 { 26 return "", path 27 } 28 29 return string(path[:i]), string(path[i+1:]) 30 } 31 32 // Dir returns all but the last element of path, typically the path's directory. 33 // - path is the path to be split. 34 func Dir(path string) string { 35 dir, _ := Split(path) 36 37 return dir 38 } 39 40 // Join joins any number of path elements into a single path, 41 // separating them with slash. Empty elements are ignored. 42 // The result is Cleaned. However, if the argument 43 // list is empty or all its elements are empty, Join returns 44 // an empty string. 45 func Join(elem ...string) string { 46 var items []string 47 var hasElements bool 48 for _, e := range elem { 49 if e != "" { 50 hasElements = true 51 for _, it := range strings.Split(e, "/") { 52 if it != "" { 53 items = append(items, it) 54 } 55 } 56 } 57 } 58 if !hasElements { 59 return "" 60 } 61 62 return "/" + strings.Join(items, "/") 63 64 }