version: 1.10

package path

import "path"

Overview

Package path implements utility routines for manipulating slash-separated paths.

The path package should only be used for paths separated by forward slashes,
such as the paths in URLs. This package does not deal with Windows paths with
drive letters or backslashes; to manipulate operating system paths, use the
path/filepath package.

Index

Examples

Package files

match.go path.go

Variables

  1. var ErrBadPattern = errors.New("syntax error in pattern")

ErrBadPattern indicates a globbing pattern was malformed.

func Base

  1. func Base(path string) string

Base returns the last element of path. Trailing slashes are removed before
extracting the last element. If the path is empty, Base returns “.”. If the path
consists entirely of slashes, Base returns “/“.


Example:

  1. fmt.Println(path.Base("/a/b"))
  2. fmt.Println(path.Base("/"))
  3. fmt.Println(path.Base(""))
  4. // Output:
  5. // b
  6. // /
  7. // .

func Clean

  1. func Clean(path string) string

Clean returns the shortest path name equivalent to path by purely lexical
processing. It applies the following rules iteratively until no further
processing can be done:

  1. 1. Replace multiple slashes with a single slash.
  2. 2. Eliminate each . path name element (the current directory).
  3. 3. Eliminate each inner .. path name element (the parent directory)
  4. along with the non-.. element that precedes it.
  5. 4. Eliminate .. elements that begin a rooted path:
  6. that is, replace "/.." by "/" at the beginning of a path.

The returned path ends in a slash only if it is the root “/“.

If the result of this process is an empty string, Clean returns the string “.”.

See also Rob Pike, ``Lexical File Names in Plan 9 or Getting Dot-Dot Right,’’
https://9p.io/sys/doc/lexnames.html


Example:

  1. paths := []string{
  2. "a/c",
  3. "a//c",
  4. "a/c/.",
  5. "a/c/b/..",
  6. "/../a/c",
  7. "/../a/b/../././/c",
  8. "",
  9. }
  10. for _, p := range paths {
  11. fmt.Printf("Clean(%q) = %q\n", p, path.Clean(p))
  12. }
  13. // Output:
  14. // Clean("a/c") = "a/c"
  15. // Clean("a//c") = "a/c"
  16. // Clean("a/c/.") = "a/c"
  17. // Clean("a/c/b/..") = "a/c"
  18. // Clean("/../a/c") = "/a/c"
  19. // Clean("/../a/b/../././/c") = "/a/c"
  20. // Clean("") = "."

func Dir

  1. func Dir(path string) string

Dir returns all but the last element of path, typically the path’s directory.
After dropping the final element using Split, the path is Cleaned and trailing
slashes are removed. If the path is empty, Dir returns “.”. If the path consists
entirely of slashes followed by non-slash bytes, Dir returns a single slash. In
any other case, the returned path does not end in a slash.


Example:

  1. fmt.Println(path.Dir("/a/b/c"))
  2. fmt.Println(path.Dir("a/b/c"))
  3. fmt.Println(path.Dir("/a/"))
  4. fmt.Println(path.Dir("a/"))
  5. fmt.Println(path.Dir("/"))
  6. fmt.Println(path.Dir(""))
  7. // Output:
  8. // /a/b
  9. // a/b
  10. // /a
  11. // a
  12. // /
  13. // .

func Ext

  1. func Ext(path string) string

Ext returns the file name extension used by path. The extension is the suffix
beginning at the final dot in the final slash-separated element of path; it is
empty if there is no dot.


Example:

  1. fmt.Println(path.Ext("/a/b/c/bar.css"))
  2. fmt.Println(path.Ext("/"))
  3. fmt.Println(path.Ext(""))
  4. // Output:
  5. // .css
  6. //
  7. //

func IsAbs

  1. func IsAbs(path string) bool

IsAbs reports whether the path is absolute.


Example:

  1. fmt.Println(path.IsAbs("/dev/null"))
  2. // Output: true

func Join

  1. func Join(elem ...string) string

Join joins any number of path elements into a single path, adding a separating
slash if necessary. The result is Cleaned; in particular, all empty strings are
ignored.


Example:

  1. fmt.Println(path.Join("a", "b", "c"))
  2. fmt.Println(path.Join("a", "b/c"))
  3. fmt.Println(path.Join("a/b", "c"))
  4. fmt.Println(path.Join("", ""))
  5. fmt.Println(path.Join("a", ""))
  6. fmt.Println(path.Join("", "a"))
  7. // Output:
  8. // a/b/c
  9. // a/b/c
  10. // a/b/c
  11. //
  12. // a
  13. // a

func Match

  1. func Match(pattern, name string) (matched bool, err error)

Match reports whether name matches the shell file name pattern. The pattern
syntax is:

  1. pattern:
  2. { term }
  3. term:
  4. '*' matches any sequence of non-/ characters
  5. '?' matches any single non-/ character
  6. '[' [ '^' ] { character-range } ']'
  7. character class (must be non-empty)
  8. c matches character c (c != '*', '?', '\\', '[')
  9. '\\' c matches character c
  10. character-range:
  11. c matches character c (c != '\\', '-', ']')
  12. '\\' c matches character c
  13. lo '-' hi matches character c for lo <= c <= hi

Match requires pattern to match all of name, not just a substring. The only
possible returned error is ErrBadPattern, when pattern is malformed.

func Split

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

Split splits path immediately following the final slash, separating it into a
directory and file name component. If there is no slash in path, Split returns
an empty dir and file set to path. The returned values have the property that
path = dir+file.


Example:

  1. fmt.Println(path.Split("static/myfile.css"))
  2. fmt.Println(path.Split("myfile.css"))
  3. fmt.Println(path.Split(""))
  4. // Output:
  5. // static/ myfile.css
  6. // myfile.css
  7. //

Subdirectories