path.Clean
Returns the shortest path name equivalent to the given path.
- Syntax
- path.Clean PATH
- Returns
- string
Usage
The path.Clean function returns the shortest path name equivalent to the given path. The function processes the path as text and does not access the file system. It applies these rules until it can make no further changes:
- Replace multiple slashes with a single slash.
- Remove each
.element, which refers to the current directory. - Remove each
..element, which refers to the parent directory, along with the element before it. - Remove each
..element that begins a rooted path, replacing/..with/.
The returned path ends in a slash only if it is the root directory (/).
On Windows, the function first replaces each backslash (\) in its arguments with a slash (/).
Examples
{{ path.Clean "foo/bar" }} → foo/bar
{{ path.Clean "/foo/bar" }} → /foo/bar
{{ path.Clean "/foo/bar/" }} → /foo/bar
{{ path.Clean "/foo//bar/" }} → /foo/bar
{{ path.Clean "/foo/./bar/" }} → /foo/bar
{{ path.Clean "/foo/../bar/" }} → /bar
{{ path.Clean "/../foo/../bar/" }} → /barRelative paths that begin with a .. element:
{{ path.Clean "../foo" }} → ../foo
{{ path.Clean "../foo/../bar" }} → ../barEmpty paths and the root directory:
{{ path.Clean "" }} → .
{{ path.Clean "/" }} → /Last updated:
November 12, 2026
:
content: Apply style guide to path functions (0fd00f291)
Improve this page