The Wayback Machine - https://web.archive.org/web/20110525035625/http://golang.org:80/pkg/path/filepath/

The Go Programming Language

Package filepath

import "path/filepath"

Package filepath implements utility routines for manipulating filename paths in a way compatible with the target operating system-defined file paths.

Package files

match.go path.go path_plan9.go path_unix.go path_windows.go

Constants

Image for: Constants
const (
    Separator     = '/' // OS-specific path separator
    ListSeparator = 0   // OS-specific path list separator
)
const (
    Separator     = '\\' // OS-specific path separator
    ListSeparator = ':'  // OS-specific path list separator
)
const (
    Separator     = '/' // OS-specific path separator
    ListSeparator = ':' // OS-specific path list separator
)
const (
    SeparatorString     = string(Separator)
    ListSeparatorString = string(ListSeparator)
)

Variables

Image for: Variables
var ErrBadPattern = os.NewError("syntax error in pattern")

func Abs

Image for: func Abs

func Abs(path string) (string, os.Error)

Abs returns an absolute representation of path. If the path is not absolute it will be joined with the current working directory to turn it into an absolute path. The absolute path name for a given file is not guaranteed to be unique.

func Base

Image for: func Base

func Base(path string) string

Base returns the last element of path. Trailing path separators are removed before extracting the last element. If the path is empty, Base returns ".". If the path consists entirely of separators, Base returns a single separator.

func Clean

Image for: func Clean

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. Replace multiple Separator elements with a single one.
	2. Eliminate each . path name element (the current directory).
	3. Eliminate each inner .. path name element (the parent directory)
	   along with the non-.. element that precedes it.
	4. Eliminate .. elements that begin a rooted path:
	   that is, replace "/.." by "/" at the beginning of a path,
        assuming Separator is '/'.

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,” http://plan9.bell-labs.com/sys/doc/lexnames.html

Image for: func EvalSymlinks

func EvalSymlinks(path string) (string, os.Error)

EvalSymlinks returns the path name after the evaluation of any symbolic links. If path is relative it will be evaluated relative to the current directory.

func Ext

Image for: func Ext

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 element of path; it is empty if there is no dot.

func FromSlash

Image for: func FromSlash

func FromSlash(path string) string

FromSlash returns the result of replacing each slash ('/') character in path with a separator character.

func Glob

Image for: func Glob

func Glob(pattern string) (matches []string, err os.Error)

Glob returns the names of all files matching pattern or nil if there is no matching file. The syntax of patterns is the same as in Match. The pattern may describe hierarchical names such as /usr/*/bin/ed (assuming the Separator is '/'). The only possible error return occurs when the pattern is malformed.

func IsAbs

Image for: func IsAbs

func IsAbs(path string) bool

IsAbs returns true if the path is absolute.

func Join

Image for: func Join

func Join(elem ...string) string

Join joins any number of path elements into a single path, adding a Separator if necessary. All empty strings are ignored.

func Match

Image for: func Match

func Match(pattern, name string) (matched bool, err os.Error)

Match returns true if name matches the shell file name pattern. The pattern syntax is:

pattern:
	{ term }
term:
	'*'         matches any sequence of non-Separator characters
	'?'         matches any single non-Separator character
	'[' [ '^' ] { character-range } ']'
	            character class (must be non-empty)
	c           matches character c (c != '*', '?', '\\', '[')
	'\\' c      matches character c

character-range:
	c           matches character c (c != '\\', '-', ']')
	'\\' c      matches character c
	lo '-' hi   matches character c for lo <= c <= hi

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

func Split

Image for: func Split

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

Split splits path immediately following the final Separator, partitioning it into a directory and a file name components. If there are no separators in path, Split returns an empty base and file set to path.

func SplitList

Image for: func SplitList

func SplitList(path string) []string

SplitList splits a list of paths joined by the OS-specific ListSeparator.

func ToSlash

Image for: func ToSlash

func ToSlash(path string) string

ToSlash returns the result of replacing each separator character in path with a slash ('/') character.

func Walk

Image for: func Walk

func Walk(root string, v Visitor, errors chan<- os.Error)

Walk walks the file tree rooted at root, calling v.VisitDir or v.VisitFile for each directory or file in the tree, including root. If v.VisitDir returns false, Walk skips the directory's entries; otherwise it invokes itself for each directory entry in sorted order. An error reading a directory does not abort the Walk. If errors != nil, Walk sends each directory read error to the channel. Otherwise Walk discards the error.

type Visitor

Image for: type Visitor

Visitor methods are invoked for corresponding file tree entries visited by Walk. The parameter path is the full path of f relative to root.

type Visitor interface {
    VisitDir(path string, f *os.FileInfo) bool
    VisitFile(path string, f *os.FileInfo)
}

release.r57.1 8276. Except as noted, this content is licensed under a Creative Commons Attribution 3.0 License.