github.com/hairyhenderson/gomplate/v4@v4.0.0-pre-2.0.20240520121557-362f058f0c93/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 _Added in gomplate [v2.7.0](https://github.com/hairyhenderson/gomplate/releases/tag/v2.7.0)_ 23 ### Usage 24 25 ``` 26 path.Base path 27 ``` 28 ``` 29 path | path.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 '{{ path.Base "/tmp/foo" }}' 42 foo 43 ``` 44 45 ## `path.Clean` 46 47 Clean returns the shortest path name equivalent to path by purely lexical processing. 48 49 A wrapper for Go's [`path.Clean`](https://golang.org/pkg/path/#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 path.Clean path 56 ``` 57 ``` 58 path | path.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 '{{ path.Clean "/tmp//foo/../" }}' 71 /tmp 72 ``` 73 74 ## `path.Dir` 75 76 Returns all but the last element of path, typically the path's directory. 77 78 A wrapper for Go's [`path.Dir`](https://golang.org/pkg/path/#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 path.Dir path 85 ``` 86 ``` 87 path | path.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 '{{ path.Dir "/tmp/foo" }}' 100 /tmp 101 ``` 102 103 ## `path.Ext` 104 105 Returns the file name extension used by path. 106 107 A wrapper for Go's [`path.Ext`](https://golang.org/pkg/path/#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 path.Ext path 114 ``` 115 ``` 116 path | path.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 '{{ path.Ext "/tmp/foo.csv" }}' 129 .csv 130 ``` 131 132 ## `path.IsAbs` 133 134 Reports whether the path is absolute. 135 136 A wrapper for Go's [`path.IsAbs`](https://golang.org/pkg/path/#IsAbs) function. 137 138 _Added in gomplate [v2.7.0](https://github.com/hairyhenderson/gomplate/releases/tag/v2.7.0)_ 139 ### Usage 140 141 ``` 142 path.IsAbs path 143 ``` 144 ``` 145 path | path.IsAbs 146 ``` 147 148 ### Arguments 149 150 | name | description | 151 |------|-------------| 152 | `path` | _(required)_ The input path | 153 154 ### Examples 155 156 ```console 157 $ gomplate -i 'the path is {{ if (path.IsAbs "/tmp/foo.csv") }}absolute{{else}}relative{{end}}' 158 the path is absolute 159 $ gomplate -i 'the path is {{ if (path.IsAbs "../foo.csv") }}absolute{{else}}relative{{end}}' 160 the path is relative 161 ``` 162 163 ## `path.Join` 164 165 Joins any number of path elements into a single path, adding a separating slash if necessary. 166 167 A wrapper for Go's [`path.Join`](https://golang.org/pkg/path/#Join) function. 168 169 _Added in gomplate [v2.7.0](https://github.com/hairyhenderson/gomplate/releases/tag/v2.7.0)_ 170 ### Usage 171 172 ``` 173 path.Join elem... 174 ``` 175 176 ### Arguments 177 178 | name | description | 179 |------|-------------| 180 | `elem...` | _(required)_ The path elements to join (0 or more) | 181 182 ### Examples 183 184 ```console 185 $ gomplate -i '{{ path.Join "/tmp" "foo" "bar" }}' 186 /tmp/foo/bar 187 ``` 188 189 ## `path.Match` 190 191 Reports whether name matches the shell file name pattern. 192 193 A wrapper for Go's [`path.Match`](https://golang.org/pkg/path/#Match) function. 194 195 _Added in gomplate [v2.7.0](https://github.com/hairyhenderson/gomplate/releases/tag/v2.7.0)_ 196 ### Usage 197 198 ``` 199 path.Match pattern path 200 ``` 201 202 ### Arguments 203 204 | name | description | 205 |------|-------------| 206 | `pattern` | _(required)_ The pattern to match on | 207 | `path` | _(required)_ The path to match | 208 209 ### Examples 210 211 ```console 212 $ gomplate -i '{{ path.Match "*.csv" "foo.csv" }}' 213 true 214 ``` 215 216 ## `path.Split` 217 218 Splits path immediately following the final slash, separating it into a directory and file name component. 219 220 The function returns an array with two values, the first being the directory, and the second the file. 221 222 A wrapper for Go's [`path.Split`](https://golang.org/pkg/path/#Split) function. 223 224 _Added in gomplate [v2.7.0](https://github.com/hairyhenderson/gomplate/releases/tag/v2.7.0)_ 225 ### Usage 226 227 ``` 228 path.Split path 229 ``` 230 ``` 231 path | path.Split 232 ``` 233 234 ### Arguments 235 236 | name | description | 237 |------|-------------| 238 | `path` | _(required)_ The input path | 239 240 ### Examples 241 242 ```console 243 $ gomplate -i '{{ $p := path.Split "/tmp/foo" }}{{ $dir := index $p 0 }}{{ $file := index $p 1 }}dir is {{$dir}}, file is {{$file}}' 244 dir is /tmp/, file is foo 245 ```