HUGO
Menu
GitHub 90498 stars Mastodon

path.Dir

Returns all but the last element of the given path.
Syntax
path.Dir PATH
Returns
string

Usage

The path.Dir function returns all but the last element of the given path, typically the path’s directory. After the function removes the last element, it cleans the result as described in the path.Clean function, which removes trailing slashes.

If the path is empty or does not contain a slash, the function returns .. If the path contains only slashes followed by a single element, the function returns /. Otherwise, the returned path does not end in a slash.

On Windows, the function first replaces each backslash (\) in its arguments with a slash (/).

Examples

{{ path.Dir "a/news.html" }} → a
{{ path.Dir "a/b/c" }} → a/b
{{ path.Dir "/a/b/c" }} → /a/b
{{ path.Dir "/a/b/c/" }} → /a/b/c

Paths with one element or no elements:

{{ path.Dir "news.html" }} → .
{{ path.Dir "/news.html" }} → /
{{ path.Dir "" }} → .