HUGO
Menu
GitHub 90498 stars Mastodon

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:

  1. Replace multiple slashes with a single slash.
  2. Remove each . element, which refers to the current directory.
  3. Remove each .. element, which refers to the parent directory, along with the element before it.
  4. 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/" }} → /bar

Relative paths that begin with a .. element:

{{ path.Clean "../foo" }} → ../foo
{{ path.Clean "../foo/../bar" }} → ../bar

Empty paths and the root directory:

{{ path.Clean "" }} → .
{{ path.Clean "/" }} → /