HUGO
Menu
GitHub 90497 stars Mastodon

path.Split

Returns the directory and file name components of the given path, split immediately after the final slash.
Syntax
path.Split PATH
Returns
paths.DirFile

Usage

The path.Split function returns a DirFile object containing the directory and file name components of the given path, split immediately after the final slash. Use these methods on the DirFile object:

Dir
(string) Returns the directory component, including the final slash. If the path does not contain a slash, returns an empty string.
File
(string) Returns the file name component. If the path ends with a slash, returns an empty string.

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

Examples

{{ $dirFile := path.Split "a/news.html" }}
{{ $dirFile.Dir }} → a/
{{ $dirFile.File }} → news.html

{{ $dirFile := path.Split "a/b/c" }}
{{ $dirFile.Dir }} → a/b/
{{ $dirFile.File }} → c

A path without a slash:

{{ $dirFile := path.Split "news.html" }}
{{ $dirFile.Dir }} → "" (empty string)
{{ $dirFile.File }} → news.html

A path that ends with a slash:

{{ $dirFile := path.Split "a/b/" }}
{{ $dirFile.Dir }} → a/b/
{{ $dirFile.File }} → "" (empty string)