Documentation
¶
Index ¶
- func Canonical(path string) string
- func CleanCurrentDirs(path string) string
- func CleanSeparators(path string) string
- func Contains(candidate string, path string) bool
- func Ext(path string) string
- func IsAbs(path string) bool
- func Join(parts ...string) string
- func Parent(path string) string
- func RemoveTrailingSeparators(path string) string
- func Split(path string) (dir string, file string)
- func Stem(path string) string
- func Top(path string) string
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Canonical ¶
Canonical returns path in canonical form.
Example ¶
package main
import (
"fmt"
"github.com/reiver/go-path"
)
func main() {
var paths []string = []string{
"",
"/",
".",
"..",
"/../../..////./././../something/././/..///./wow/././pow/./.",
}
for _, p := range paths {
canonical := path.Canonical(p)
fmt.Printf("%q -> %q\n", p, canonical)
}
}
Output: "" -> "." "/" -> "/" "." -> "." ".." -> ".." "/../../..////./././../something/././/..///./wow/././pow/./." -> "/wow/pow/"
func CleanCurrentDirs ¶
CleanCurrentDirs returns the path with any extra current directory references removed.
Example ¶
package main
import (
"fmt"
"github.com/reiver/go-path"
)
func main() {
var paths []string = []string{
"/",
"/.",
"./",
"/./",
"/././",
"/./././",
"././",
"./././",
"././././",
"/./.",
"/././.",
"/./././.",
"/././././././././dir/././././././file.txt",
"././././././././dir/././././././file.txt",
}
for _, p := range paths {
cleanedPath := path.CleanCurrentDirs(p)
fmt.Printf("%q -> %q\n", p, cleanedPath)
}
}
Output: "/" -> "/" "/." -> "/" "./" -> "." "/./" -> "/" "/././" -> "/" "/./././" -> "/" "././" -> "." "./././" -> "." "././././" -> "." "/./." -> "/" "/././." -> "/" "/./././." -> "/" "/././././././././dir/././././././file.txt" -> "/dir/file.txt" "././././././././dir/././././././file.txt" -> "dir/file.txt"
func CleanSeparators ¶
CleanSeparators returns path with any extra consecutive separators removed.
Example ¶
package main
import (
"fmt"
"github.com/reiver/go-path"
)
func main() {
var paths []string = []string{
"/",
"//",
"///",
"////",
"something",
"something/",
"something//",
"something///",
"something////",
"/////something",
"////something/",
"///something//",
"//something///",
"/something////",
"//////once/////twice////thrice///fource//",
}
for _, p := range paths {
cleanedPath := path.CleanSeparators(p)
fmt.Printf("%q -> %q\n", p, cleanedPath)
}
}
Output: "/" -> "/" "//" -> "/" "///" -> "/" "////" -> "/" "something" -> "something" "something/" -> "something/" "something//" -> "something/" "something///" -> "something/" "something////" -> "something/" "/////something" -> "/something" "////something/" -> "/something/" "///something//" -> "/something/" "//something///" -> "/something/" "/something////" -> "/something/" "//////once/////twice////thrice///fource//" -> "/once/twice/thrice/fource/"
func Contains ¶
Contains returns true if the candidate contains the path.
So, for example, this:
path.Contains("/once/twice", "/once/twice/thrice/fource")
... would return true.
func Ext ¶
Ext returns the file-name extension if it exists, and return an empty string otherwise.
Example ¶
package main
import (
"fmt"
"github.com/reiver/go-path"
)
func main() {
var paths []string = []string{
"",
"/",
"/doc.txt",
"doc.txt",
"/image.jpeg",
"image.jpeg",
"/apple/banana/cherry.html",
"apple/banana/cherry.html",
"/path/to/archive.tar.gz",
"path/to/archive.tar.gz",
"/once/twice/thrice/fource",
"once/twice/thrice/fource",
"/LICENSE",
"LICENSE",
"/src/README.md",
"src/README.md",
"/ALLCAPS.HTML",
"ALLCAPS.HTML",
"/something/name.dir/filename",
"something/name.dir/filename",
}
for _, p := range paths {
extension := path.Ext(p)
fmt.Printf("the 'extension' of the path %q is %q\n", p, extension)
}
}
Output: the 'extension' of the path "" is "" the 'extension' of the path "/" is "" the 'extension' of the path "/doc.txt" is ".txt" the 'extension' of the path "doc.txt" is ".txt" the 'extension' of the path "/image.jpeg" is ".jpeg" the 'extension' of the path "image.jpeg" is ".jpeg" the 'extension' of the path "/apple/banana/cherry.html" is ".html" the 'extension' of the path "apple/banana/cherry.html" is ".html" the 'extension' of the path "/path/to/archive.tar.gz" is ".gz" the 'extension' of the path "path/to/archive.tar.gz" is ".gz" the 'extension' of the path "/once/twice/thrice/fource" is "" the 'extension' of the path "once/twice/thrice/fource" is "" the 'extension' of the path "/LICENSE" is "" the 'extension' of the path "LICENSE" is "" the 'extension' of the path "/src/README.md" is ".md" the 'extension' of the path "src/README.md" is ".md" the 'extension' of the path "/ALLCAPS.HTML" is ".HTML" the 'extension' of the path "ALLCAPS.HTML" is ".HTML" the 'extension' of the path "/something/name.dir/filename" is "" the 'extension' of the path "something/name.dir/filename" is ""
func Join ¶
Join returns the canonical form of the parts connected together.
Example ¶
package main
import (
"fmt"
"github.com/reiver/go-path"
)
func main() {
var list [][]string = [][]string{
[]string{},
[]string{""},
[]string{"", ""},
[]string{"/"},
[]string{"/", "one"},
[]string{"/", "one", "two"},
[]string{"/", "one", "two", "three"},
[]string{"apple"},
[]string{"apple", "banana"},
[]string{"apple", "banana", "cherry"},
[]string{"/path/to", "file.txt"},
[]string{"/path/to/", "file.txt"},
[]string{"/once/twice/thrice/fource.png"},
[]string{"/once/twice/thrice/", "fource.png"},
[]string{"/once/twice/thrice", "fource.png"},
[]string{"/once/twice/", "thrice/fource.png"},
[]string{"/once/twice", "thrice/fource.png"},
}
for _, parts := range list {
joined := path.Join(parts...)
printResult(joined, parts...)
}
}
func printResult(joined string, parts ...string) {
fmt.Print("path.Join(")
for i, part := range parts {
if 0 < i {
fmt.Print(", ")
}
fmt.Printf("%q", part)
}
fmt.Printf(") -> %q\n", joined)
}
Output: path.Join() -> "." path.Join("") -> "." path.Join("", "") -> "." path.Join("/") -> "/" path.Join("/", "one") -> "/one" path.Join("/", "one", "two") -> "/one/two" path.Join("/", "one", "two", "three") -> "/one/two/three" path.Join("apple") -> "apple" path.Join("apple", "banana") -> "apple/banana" path.Join("apple", "banana", "cherry") -> "apple/banana/cherry" path.Join("/path/to", "file.txt") -> "/path/to/file.txt" path.Join("/path/to/", "file.txt") -> "/path/to/file.txt" path.Join("/once/twice/thrice/fource.png") -> "/once/twice/thrice/fource.png" path.Join("/once/twice/thrice/", "fource.png") -> "/once/twice/thrice/fource.png" path.Join("/once/twice/thrice", "fource.png") -> "/once/twice/thrice/fource.png" path.Join("/once/twice/", "thrice/fource.png") -> "/once/twice/thrice/fource.png" path.Join("/once/twice", "thrice/fource.png") -> "/once/twice/thrice/fource.png"
func Parent ¶
Parent returns the parent directory of path.
Example ¶
package main
import (
"fmt"
"github.com/reiver/go-path"
)
func main() {
var paths []string = []string{
"/",
".",
"..",
"../..",
"/image.jpeg",
"image.jpeg",
"/apple/banana/cherry.html",
"apple/banana/cherry.html",
}
for _, p := range paths {
extension := path.Parent(p)
fmt.Printf("%q -> %q\n", p, extension)
}
}
Output: "/" -> "/" "." -> ".." ".." -> "../.." "../.." -> "../../.." "/image.jpeg" -> "/" "image.jpeg" -> "." "/apple/banana/cherry.html" -> "/apple/banana/" "apple/banana/cherry.html" -> "apple/banana/"
func RemoveTrailingSeparators ¶
RemoveTrailingSeparators returns the path with any trailing separator characters removed.
func Split ¶
Split splits a path into the directory part and file part. It does NOT do any clean-up (such as removing the trailing slash on the directory).
Note that whether there is a trailing separator (i.e., "/") character or not has an effect on the result.
For example:
"/once/twice/thrice/fource" -> dir: "/once/twice/thrice/", file: "fource" "/once/twice/thrice/fource/" -> dir: "/once/twice/thrice/fource/", file: ""
func Stem ¶
Stem returns the file-name without the extension if it exists, and return an empty string otherwise.
Note that if 'path' == "archive.tar.gz", then the stem would be "archive.tar" (rather than just "archive"). But the stem of 'path' == "archive.tar" would be "archive".
Example ¶
package main
import (
"fmt"
"github.com/reiver/go-path"
)
func main() {
var paths []string = []string{
"",
"/",
"/doc.txt",
"doc.txt",
"/image.jpeg",
"image.jpeg",
"/apple/banana/cherry.html",
"apple/banana/cherry.html",
"/path/to/archive.tar.gz",
"path/to/archive.tar.gz",
"/once/twice/thrice/fource",
"once/twice/thrice/fource",
"/LICENSE",
"LICENSE",
"/src/README.md",
"src/README.md",
"/ALLCAPS.HTML",
"ALLCAPS.HTML",
"/something/name.dir/filename",
"something/name.dir/filename",
}
for _, p := range paths {
extension := path.Stem(p)
fmt.Printf("the 'extension' of the path %q is %q\n", p, extension)
}
}
Output: the 'extension' of the path "" is "" the 'extension' of the path "/" is "" the 'extension' of the path "/doc.txt" is "doc" the 'extension' of the path "doc.txt" is "doc" the 'extension' of the path "/image.jpeg" is "image" the 'extension' of the path "image.jpeg" is "image" the 'extension' of the path "/apple/banana/cherry.html" is "cherry" the 'extension' of the path "apple/banana/cherry.html" is "cherry" the 'extension' of the path "/path/to/archive.tar.gz" is "archive.tar" the 'extension' of the path "path/to/archive.tar.gz" is "archive.tar" the 'extension' of the path "/once/twice/thrice/fource" is "fource" the 'extension' of the path "once/twice/thrice/fource" is "fource" the 'extension' of the path "/LICENSE" is "LICENSE" the 'extension' of the path "LICENSE" is "LICENSE" the 'extension' of the path "/src/README.md" is "README" the 'extension' of the path "src/README.md" is "README" the 'extension' of the path "/ALLCAPS.HTML" is "ALLCAPS" the 'extension' of the path "ALLCAPS.HTML" is "ALLCAPS" the 'extension' of the path "/something/name.dir/filename" is "filename" the 'extension' of the path "something/name.dir/filename" is "filename"
func Top ¶
Top returns the top of a path.
Example ¶
package main
import (
"fmt"
"github.com/reiver/go-path"
)
func main() {
var paths []string = []string{
"",
"/",
"/doc.txt",
"doc.txt",
"/image.jpeg",
"image.jpeg",
"/apple/banana/cherry.html",
"apple/banana/cherry.html",
"/path/to/archive.tar.gz",
"path/to/archive.tar.gz",
"/once/twice/thrice/fource",
"once/twice/thrice/fource",
"/LICENSE",
"LICENSE",
"/src/README.md",
"src/README.md",
"/ALLCAPS.HTML",
"ALLCAPS.HTML",
"/something/name.dir/filename",
"something/name.dir/filename",
}
for _, p := range paths {
extension := path.Top(p)
fmt.Printf("the 'top' of the path %q is %q\n", p, extension)
}
}
Output: the 'top' of the path "" is "." the 'top' of the path "/" is "/" the 'top' of the path "/doc.txt" is "/" the 'top' of the path "doc.txt" is "doc.txt" the 'top' of the path "/image.jpeg" is "/" the 'top' of the path "image.jpeg" is "image.jpeg" the 'top' of the path "/apple/banana/cherry.html" is "/" the 'top' of the path "apple/banana/cherry.html" is "apple" the 'top' of the path "/path/to/archive.tar.gz" is "/" the 'top' of the path "path/to/archive.tar.gz" is "path" the 'top' of the path "/once/twice/thrice/fource" is "/" the 'top' of the path "once/twice/thrice/fource" is "once" the 'top' of the path "/LICENSE" is "/" the 'top' of the path "LICENSE" is "LICENSE" the 'top' of the path "/src/README.md" is "/" the 'top' of the path "src/README.md" is "src" the 'top' of the path "/ALLCAPS.HTML" is "/" the 'top' of the path "ALLCAPS.HTML" is "ALLCAPS.HTML" the 'top' of the path "/something/name.dir/filename" is "/" the 'top' of the path "something/name.dir/filename" is "something"
Types ¶
This section is empty.