github.com/hairyhenderson/gomplate/v4@v4.0.0-pre-2.0.20240520121557-362f058f0c93/docs/content/functions/filepath.md (about)

     1  ---
     2  title: filepath functions
     3  menu:
     4    main:
     5      parent: functions
     6  ---
     7  
     8  gomplate's path functions are split into 2 namespaces:
     9  - `path`, which is useful for manipulating slash-based (`/`) paths, such as in URLs
    10  - `filepath`, which should be used for local filesystem paths, especially when Windows paths may be involved.
    11  
    12  This page documents the `filepath` namespace - see also the [`path`](../path) documentation.
    13  
    14  These functions are wrappers for Go's [`path/filepath`](https://golang.org/pkg/path/filepath/) package.
    15  
    16  ## `filepath.Base`
    17  
    18  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.
    19  
    20  A wrapper for Go's [`filepath.Base`](https://golang.org/pkg/path/filepath/#Base) function.
    21  
    22  _Added in gomplate [v2.7.0](https://github.com/hairyhenderson/gomplate/releases/tag/v2.7.0)_
    23  ### Usage
    24  
    25  ```
    26  filepath.Base path
    27  ```
    28  ```
    29  path | filepath.Base
    30  ```
    31  
    32  ### Arguments
    33  
    34  | name | description |
    35  |------|-------------|
    36  | `path` | _(required)_ The input path |
    37  
    38  ### Examples
    39  
    40  ```console
    41  $ gomplate -i '{{ filepath.Base "/tmp/foo" }}'
    42  foo
    43  ```
    44  
    45  ## `filepath.Clean`
    46  
    47  Clean returns the shortest path name equivalent to path by purely lexical processing.
    48  
    49  A wrapper for Go's [`filepath.Clean`](https://golang.org/pkg/path/filepath/#Clean) function.
    50  
    51  _Added in gomplate [v2.7.0](https://github.com/hairyhenderson/gomplate/releases/tag/v2.7.0)_
    52  ### Usage
    53  
    54  ```
    55  filepath.Clean path
    56  ```
    57  ```
    58  path | filepath.Clean
    59  ```
    60  
    61  ### Arguments
    62  
    63  | name | description |
    64  |------|-------------|
    65  | `path` | _(required)_ The input path |
    66  
    67  ### Examples
    68  
    69  ```console
    70  $ gomplate -i '{{ filepath.Clean "/tmp//foo/../" }}'
    71  /tmp
    72  ```
    73  
    74  ## `filepath.Dir`
    75  
    76  Returns all but the last element of path, typically the path's directory.
    77  
    78  A wrapper for Go's [`filepath.Dir`](https://golang.org/pkg/path/filepath/#Dir) function.
    79  
    80  _Added in gomplate [v2.7.0](https://github.com/hairyhenderson/gomplate/releases/tag/v2.7.0)_
    81  ### Usage
    82  
    83  ```
    84  filepath.Dir path
    85  ```
    86  ```
    87  path | filepath.Dir
    88  ```
    89  
    90  ### Arguments
    91  
    92  | name | description |
    93  |------|-------------|
    94  | `path` | _(required)_ The input path |
    95  
    96  ### Examples
    97  
    98  ```console
    99  $ gomplate -i '{{ filepath.Dir "/tmp/foo" }}'
   100  /tmp
   101  ```
   102  
   103  ## `filepath.Ext`
   104  
   105  Returns the file name extension used by path.
   106  
   107  A wrapper for Go's [`filepath.Ext`](https://golang.org/pkg/path/filepath/#Ext) function.
   108  
   109  _Added in gomplate [v2.7.0](https://github.com/hairyhenderson/gomplate/releases/tag/v2.7.0)_
   110  ### Usage
   111  
   112  ```
   113  filepath.Ext path
   114  ```
   115  ```
   116  path | filepath.Ext
   117  ```
   118  
   119  ### Arguments
   120  
   121  | name | description |
   122  |------|-------------|
   123  | `path` | _(required)_ The input path |
   124  
   125  ### Examples
   126  
   127  ```console
   128  $ gomplate -i '{{ filepath.Ext "/tmp/foo.csv" }}'
   129  .csv
   130  ```
   131  
   132  ## `filepath.FromSlash`
   133  
   134  Returns the result of replacing each slash (`/`) character in the path with the platform's separator character.
   135  
   136  A wrapper for Go's [`filepath.FromSlash`](https://golang.org/pkg/path/filepath/#FromSlash) function.
   137  
   138  _Added in gomplate [v2.7.0](https://github.com/hairyhenderson/gomplate/releases/tag/v2.7.0)_
   139  ### Usage
   140  
   141  ```
   142  filepath.FromSlash path
   143  ```
   144  ```
   145  path | filepath.FromSlash
   146  ```
   147  
   148  ### Arguments
   149  
   150  | name | description |
   151  |------|-------------|
   152  | `path` | _(required)_ The input path |
   153  
   154  ### Examples
   155  
   156  ```console
   157  $ gomplate -i '{{ filepath.FromSlash "/foo/bar" }}'
   158  /foo/bar
   159  C:\> gomplate.exe -i '{{ filepath.FromSlash "/foo/bar" }}'
   160  C:\foo\bar
   161  ```
   162  
   163  ## `filepath.IsAbs`
   164  
   165  Reports whether the path is absolute.
   166  
   167  A wrapper for Go's [`filepath.IsAbs`](https://golang.org/pkg/path/filepath/#IsAbs) function.
   168  
   169  _Added in gomplate [v2.7.0](https://github.com/hairyhenderson/gomplate/releases/tag/v2.7.0)_
   170  ### Usage
   171  
   172  ```
   173  filepath.IsAbs path
   174  ```
   175  ```
   176  path | filepath.IsAbs
   177  ```
   178  
   179  ### Arguments
   180  
   181  | name | description |
   182  |------|-------------|
   183  | `path` | _(required)_ The input path |
   184  
   185  ### Examples
   186  
   187  ```console
   188  $ gomplate -i 'the path is {{ if (filepath.IsAbs "/tmp/foo.csv") }}absolute{{else}}relative{{end}}'
   189  the path is absolute
   190  $ gomplate -i 'the path is {{ if (filepath.IsAbs "../foo.csv") }}absolute{{else}}relative{{end}}'
   191  the path is relative
   192  ```
   193  
   194  ## `filepath.Join`
   195  
   196  Joins any number of path elements into a single path, adding a separator if necessary.
   197  
   198  A wrapper for Go's [`filepath.Join`](https://golang.org/pkg/path/filepath/#Join) function.
   199  
   200  _Added in gomplate [v2.7.0](https://github.com/hairyhenderson/gomplate/releases/tag/v2.7.0)_
   201  ### Usage
   202  
   203  ```
   204  filepath.Join elem...
   205  ```
   206  
   207  ### Arguments
   208  
   209  | name | description |
   210  |------|-------------|
   211  | `elem...` | _(required)_ The path elements to join (0 or more) |
   212  
   213  ### Examples
   214  
   215  ```console
   216  $ gomplate -i '{{ filepath.Join "/tmp" "foo" "bar" }}'
   217  /tmp/foo/bar
   218  C:\> gomplate.exe -i '{{ filepath.Join "C:\tmp" "foo" "bar" }}'
   219  C:\tmp\foo\bar
   220  ```
   221  
   222  ## `filepath.Match`
   223  
   224  Reports whether name matches the shell file name pattern.
   225  
   226  A wrapper for Go's [`filepath.Match`](https://golang.org/pkg/path/filepath/#Match) function.
   227  
   228  _Added in gomplate [v2.7.0](https://github.com/hairyhenderson/gomplate/releases/tag/v2.7.0)_
   229  ### Usage
   230  
   231  ```
   232  filepath.Match pattern path
   233  ```
   234  
   235  ### Arguments
   236  
   237  | name | description |
   238  |------|-------------|
   239  | `pattern` | _(required)_ The pattern to match on |
   240  | `path` | _(required)_ The path to match |
   241  
   242  ### Examples
   243  
   244  ```console
   245  $ gomplate -i '{{ filepath.Match "*.csv" "foo.csv" }}'
   246  true
   247  ```
   248  
   249  ## `filepath.Rel`
   250  
   251  Returns a relative path that is lexically equivalent to targetpath when joined to basepath with an intervening separator.
   252  
   253  A wrapper for Go's [`filepath.Rel`](https://golang.org/pkg/path/filepath/#Rel) function.
   254  
   255  _Added in gomplate [v2.7.0](https://github.com/hairyhenderson/gomplate/releases/tag/v2.7.0)_
   256  ### Usage
   257  
   258  ```
   259  filepath.Rel basepath targetpath
   260  ```
   261  
   262  ### Arguments
   263  
   264  | name | description |
   265  |------|-------------|
   266  | `basepath` | _(required)_ The base path |
   267  | `targetpath` | _(required)_ The target path |
   268  
   269  ### Examples
   270  
   271  ```console
   272  $ gomplate -i '{{ filepath.Rel "/a" "/a/b/c" }}'
   273  b/c
   274  ```
   275  
   276  ## `filepath.Split`
   277  
   278  Splits path immediately following the final path separator, separating it into a directory and file name component.
   279  
   280  The function returns an array with two values, the first being the diretory, and the second the file.
   281  
   282  A wrapper for Go's [`filepath.Split`](https://golang.org/pkg/path/filepath/#Split) function.
   283  
   284  _Added in gomplate [v2.7.0](https://github.com/hairyhenderson/gomplate/releases/tag/v2.7.0)_
   285  ### Usage
   286  
   287  ```
   288  filepath.Split path
   289  ```
   290  ```
   291  path | filepath.Split
   292  ```
   293  
   294  ### Arguments
   295  
   296  | name | description |
   297  |------|-------------|
   298  | `path` | _(required)_ The input path |
   299  
   300  ### Examples
   301  
   302  ```console
   303  $ gomplate -i '{{ $p := filepath.Split "/tmp/foo" }}{{ $dir := index $p 0 }}{{ $file := index $p 1 }}dir is {{$dir}}, file is {{$file}}'
   304  dir is /tmp/, file is foo
   305  C:\> gomplate.exe -i '{{ $p := filepath.Split `C:\tmp\foo` }}{{ $dir := index $p 0 }}{{ $file := index $p 1 }}dir is {{$dir}}, file is {{$file}}'
   306  dir is C:\tmp\, file is foo
   307  ```
   308  
   309  ## `filepath.ToSlash`
   310  
   311  Returns the result of replacing each separator character in path with a slash (`/`) character.
   312  
   313  A wrapper for Go's [`filepath.ToSlash`](https://golang.org/pkg/path/filepath/#ToSlash) function.
   314  
   315  _Added in gomplate [v2.7.0](https://github.com/hairyhenderson/gomplate/releases/tag/v2.7.0)_
   316  ### Usage
   317  
   318  ```
   319  filepath.ToSlash path
   320  ```
   321  ```
   322  path | filepath.ToSlash
   323  ```
   324  
   325  ### Arguments
   326  
   327  | name | description |
   328  |------|-------------|
   329  | `path` | _(required)_ The input path |
   330  
   331  ### Examples
   332  
   333  ```console
   334  $ gomplate -i '{{ filepath.ToSlash "/foo/bar" }}'
   335  /foo/bar
   336  C:\> gomplate.exe -i '{{ filepath.ToSlash `foo\bar\baz` }}'
   337  foo/bar/baz
   338  ```
   339  
   340  ## `filepath.VolumeName`
   341  
   342  Returns the leading volume name. Given `C:\foo\bar` it returns `C:` on Windows. Given a UNC like `\\host\share\foo` it returns `\\host\share`. On other platforms it returns an empty string.
   343  
   344  A wrapper for Go's [`filepath.VolumeName`](https://golang.org/pkg/path/filepath/#VolumeName) function.
   345  
   346  _Added in gomplate [v2.7.0](https://github.com/hairyhenderson/gomplate/releases/tag/v2.7.0)_
   347  ### Usage
   348  
   349  ```
   350  filepath.VolumeName path
   351  ```
   352  ```
   353  path | filepath.VolumeName
   354  ```
   355  
   356  ### Arguments
   357  
   358  | name | description |
   359  |------|-------------|
   360  | `path` | _(required)_ The input path |
   361  
   362  ### Examples
   363  
   364  ```console
   365  C:\> gomplate.exe -i 'volume is {{ filepath.VolumeName "C:/foo/bar" }}'
   366  volume is C:
   367  $ gomplate -i 'volume is {{ filepath.VolumeName "/foo/bar" }}'
   368  volume is
   369  ```