github.com/wuhuizuo/gomplate@v3.5.0+incompatible/docs/content/functions/path.md (about)

     1  ---
     2  title: path 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 `path` namespace - see also the [`filepath`](../filepath) documentation.
    13  
    14  These functions are wrappers for Go's [`path`](https://golang.org/pkg/path/) and [`path/filepath`](https://golang.org/pkg/path/filepath/) packages.
    15  
    16  ## `path.Base`
    17  
    18  Returns the last element of path. Trailing slashes are removed before extracting the last element. If the path is empty, Base returns `.`. If the path consists entirely of slashes, Base returns `/`.
    19  
    20  A wrapper for Go's [`path.Base`](https://golang.org/pkg/path/#Base) function.
    21  
    22  ### Usage
    23  
    24  ```go
    25  path.Base path
    26  ```
    27  ```go
    28  path | path.Base
    29  ```
    30  
    31  ### Arguments
    32  
    33  | name | description |
    34  |------|-------------|
    35  | `path` | _(required)_ The input path |
    36  
    37  ### Examples
    38  
    39  ```console
    40  $ gomplate -i '{{ path.Base "/tmp/foo" }}'
    41  foo
    42  ```
    43  
    44  ## `path.Clean`
    45  
    46  Clean returns the shortest path name equivalent to path by purely lexical processing.
    47  
    48  A wrapper for Go's [`path.Clean`](https://golang.org/pkg/path/#Clean) function.
    49  
    50  ### Usage
    51  
    52  ```go
    53  path.Clean path
    54  ```
    55  ```go
    56  path | path.Clean
    57  ```
    58  
    59  ### Arguments
    60  
    61  | name | description |
    62  |------|-------------|
    63  | `path` | _(required)_ The input path |
    64  
    65  ### Examples
    66  
    67  ```console
    68  $ gomplate -i '{{ path.Clean "/tmp//foo/../" }}'
    69  /tmp
    70  ```
    71  
    72  ## `path.Dir`
    73  
    74  Returns all but the last element of path, typically the path's directory.
    75  
    76  A wrapper for Go's [`path.Dir`](https://golang.org/pkg/path/#Dir) function.
    77  
    78  ### Usage
    79  
    80  ```go
    81  path.Dir path
    82  ```
    83  ```go
    84  path | path.Dir
    85  ```
    86  
    87  ### Arguments
    88  
    89  | name | description |
    90  |------|-------------|
    91  | `path` | _(required)_ The input path |
    92  
    93  ### Examples
    94  
    95  ```console
    96  $ gomplate -i '{{ path.Dir "/tmp/foo" }}'
    97  /tmp
    98  ```
    99  
   100  ## `path.Ext`
   101  
   102  Returns the file name extension used by path.
   103  
   104  A wrapper for Go's [`path.Ext`](https://golang.org/pkg/path/#Ext) function.
   105  
   106  ### Usage
   107  
   108  ```go
   109  path.Ext path
   110  ```
   111  ```go
   112  path | path.Ext
   113  ```
   114  
   115  ### Arguments
   116  
   117  | name | description |
   118  |------|-------------|
   119  | `path` | _(required)_ The input path |
   120  
   121  ### Examples
   122  
   123  ```console
   124  $ gomplate -i '{{ path.Ext "/tmp/foo.csv" }}'
   125  .csv
   126  ```
   127  
   128  ## `path.IsAbs`
   129  
   130  Reports whether the path is absolute.
   131  
   132  A wrapper for Go's [`path.IsAbs`](https://golang.org/pkg/path/#IsAbs) function.
   133  
   134  ### Usage
   135  
   136  ```go
   137  path.IsAbs path
   138  ```
   139  ```go
   140  path | path.IsAbs
   141  ```
   142  
   143  ### Arguments
   144  
   145  | name | description |
   146  |------|-------------|
   147  | `path` | _(required)_ The input path |
   148  
   149  ### Examples
   150  
   151  ```console
   152  $ gomplate -i 'the path is {{ if (path.IsAbs "/tmp/foo.csv") }}absolute{{else}}relative{{end}}'
   153  the path is absolute
   154  $ gomplate -i 'the path is {{ if (path.IsAbs "../foo.csv") }}absolute{{else}}relative{{end}}'
   155  the path is relative
   156  ```
   157  
   158  ## `path.Join`
   159  
   160  Joins any number of path elements into a single path, adding a separating slash if necessary.
   161  
   162  A wrapper for Go's [`path.Join`](https://golang.org/pkg/path/#Join) function.
   163  
   164  ### Usage
   165  
   166  ```go
   167  path.Join elem...
   168  ```
   169  
   170  ### Arguments
   171  
   172  | name | description |
   173  |------|-------------|
   174  | `elem...` | _(required)_ The path elements to join (0 or more) |
   175  
   176  ### Examples
   177  
   178  ```console
   179  $ gomplate -i '{{ path.Join "/tmp" "foo" "bar" }}'
   180  /tmp/foo/bar
   181  ```
   182  
   183  ## `path.Match`
   184  
   185  Reports whether name matches the shell file name pattern.
   186  
   187  A wrapper for Go's [`path.Match`](https://golang.org/pkg/path/#Match) function.
   188  
   189  ### Usage
   190  
   191  ```go
   192  path.Match pattern path
   193  ```
   194  
   195  ### Arguments
   196  
   197  | name | description |
   198  |------|-------------|
   199  | `pattern` | _(required)_ The pattern to match on |
   200  | `path` | _(required)_ The path to match |
   201  
   202  ### Examples
   203  
   204  ```console
   205  $ gomplate -i '{{ path.Match "*.csv" "foo.csv" }}'
   206  true
   207  ```
   208  
   209  ## `path.Split`
   210  
   211  Splits path immediately following the final slash, separating it into a directory and file name component.
   212  
   213  The function returns an array with two values, the first being the directory, and the second the file.
   214  
   215  A wrapper for Go's [`path.Split`](https://golang.org/pkg/path/#Split) function.
   216  
   217  ### Usage
   218  
   219  ```go
   220  path.Split path
   221  ```
   222  ```go
   223  path | path.Split
   224  ```
   225  
   226  ### Arguments
   227  
   228  | name | description |
   229  |------|-------------|
   230  | `path` | _(required)_ The input path |
   231  
   232  ### Examples
   233  
   234  ```console
   235  $ gomplate -i '{{ $p := path.Split "/tmp/foo" }}{{ $dir := index $p 0 }}{{ $file := index $p 1 }}dir is {{$dir}}, file is {{$file}}'
   236  dir is /tmp/, file is foo
   237  ```