path

package module
v0.0.0-...-5f2ee05 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 27, 2024 License: MIT Imports: 2 Imported by: 8

README

go-path

Package path provides tools for working with slash-separated tools, for the Go programming language.

It is meant to be a replacement for Go's built-in "path" package. It fixes some of its bugs. And adds other useful functions.

Documention

Online documentation, which includes examples, can be found at: http://godoc.org/github.com/reiver/go-path

GoDoc

Import

To import package path use import code like the follownig:

import "github.com/reiver/go-path"

Installation

To install package path do the following:

GOPROXY=direct go get https://github.com/reiver/go-path

Author

Package path was written by Charles Iliya Krempeaux

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Canonical

func Canonical(path string) string

Canonical returns path in canonical form.

Example
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

func CleanCurrentDirs(path string) string

CleanCurrentDirs returns the path with any extra current directory references removed.

Example
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

func CleanSeparators(path string) string

CleanSeparators returns path with any extra consecutive separators removed.

Example
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

func Contains(candidate string, path string) bool

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

func Ext(path string) string

Ext returns the file-name extension if it exists, and return an empty string otherwise.

Example
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 IsAbs

func IsAbs(path string) bool

IsAbs returns if the path is an absoulte path.

An absolute path begins with a "/".

func Join

func Join(parts ...string) string

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

func Parent(path string) string

Parent returns the parent directory of path.

Example
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

func RemoveTrailingSeparators(path string) string

RemoveTrailingSeparators returns the path with any trailing separator characters removed.

func Split

func Split(path string) (dir string, file string)

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

func Stem(path string) string

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
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

func Top(path string) string

Top returns the top of a path.

Example
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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL