github.com/wuhuizuo/gomplate@v3.5.0+incompatible/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 ### Usage 23 24 ```go 25 filepath.Base path 26 ``` 27 ```go 28 path | filepath.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 '{{ filepath.Base "/tmp/foo" }}' 41 foo 42 ``` 43 44 ## `filepath.Clean` 45 46 Clean returns the shortest path name equivalent to path by purely lexical processing. 47 48 A wrapper for Go's [`filepath.Clean`](https://golang.org/pkg/path/filepath/#Clean) function. 49 50 ### Usage 51 52 ```go 53 filepath.Clean path 54 ``` 55 ```go 56 path | filepath.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 '{{ filepath.Clean "/tmp//foo/../" }}' 69 /tmp 70 ``` 71 72 ## `filepath.Dir` 73 74 Returns all but the last element of path, typically the path's directory. 75 76 A wrapper for Go's [`filepath.Dir`](https://golang.org/pkg/path/filepath/#Dir) function. 77 78 ### Usage 79 80 ```go 81 filepath.Dir path 82 ``` 83 ```go 84 path | filepath.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 '{{ filepath.Dir "/tmp/foo" }}' 97 /tmp 98 ``` 99 100 ## `filepath.Ext` 101 102 Returns the file name extension used by path. 103 104 A wrapper for Go's [`filepath.Ext`](https://golang.org/pkg/path/filepath/#Ext) function. 105 106 ### Usage 107 108 ```go 109 filepath.Ext path 110 ``` 111 ```go 112 path | filepath.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 '{{ filepath.Ext "/tmp/foo.csv" }}' 125 .csv 126 ``` 127 128 ## `filepath.FromSlash` 129 130 Returns the result of replacing each slash (`/`) character in the path with the platform's separator character. 131 132 A wrapper for Go's [`filepath.FromSlash`](https://golang.org/pkg/path/filepath/#FromSlash) function. 133 134 ### Usage 135 136 ```go 137 filepath.FromSlash path 138 ``` 139 ```go 140 path | filepath.FromSlash 141 ``` 142 143 ### Arguments 144 145 | name | description | 146 |------|-------------| 147 | `path` | _(required)_ The input path | 148 149 ### Examples 150 151 ```console 152 $ gomplate -i '{{ filepath.FromSlash "/foo/bar" }}' 153 /foo/bar 154 C:\> gomplate.exe -i '{{ filepath.FromSlash "/foo/bar" }}' 155 C:\foo\bar 156 ``` 157 158 ## `filepath.IsAbs` 159 160 Reports whether the path is absolute. 161 162 A wrapper for Go's [`filepath.IsAbs`](https://golang.org/pkg/path/filepath/#IsAbs) function. 163 164 ### Usage 165 166 ```go 167 filepath.IsAbs path 168 ``` 169 ```go 170 path | filepath.IsAbs 171 ``` 172 173 ### Arguments 174 175 | name | description | 176 |------|-------------| 177 | `path` | _(required)_ The input path | 178 179 ### Examples 180 181 ```console 182 $ gomplate -i 'the path is {{ if (filepath.IsAbs "/tmp/foo.csv") }}absolute{{else}}relative{{end}}' 183 the path is absolute 184 $ gomplate -i 'the path is {{ if (filepath.IsAbs "../foo.csv") }}absolute{{else}}relative{{end}}' 185 the path is relative 186 ``` 187 188 ## `filepath.Join` 189 190 Joins any number of path elements into a single path, adding a separator if necessary. 191 192 A wrapper for Go's [`filepath.Join`](https://golang.org/pkg/path/filepath/#Join) function. 193 194 ### Usage 195 196 ```go 197 filepath.Join elem... 198 ``` 199 200 ### Arguments 201 202 | name | description | 203 |------|-------------| 204 | `elem...` | _(required)_ The path elements to join (0 or more) | 205 206 ### Examples 207 208 ```console 209 $ gomplate -i '{{ filepath.Join "/tmp" "foo" "bar" }}' 210 /tmp/foo/bar 211 C:\> gomplate.exe -i '{{ filepath.Join "C:\tmp" "foo" "bar" }}' 212 C:\tmp\foo\bar 213 ``` 214 215 ## `filepath.Match` 216 217 Reports whether name matches the shell file name pattern. 218 219 A wrapper for Go's [`filepath.Match`](https://golang.org/pkg/path/filepath/#Match) function. 220 221 ### Usage 222 223 ```go 224 filepath.Match pattern path 225 ``` 226 227 ### Arguments 228 229 | name | description | 230 |------|-------------| 231 | `pattern` | _(required)_ The pattern to match on | 232 | `path` | _(required)_ The path to match | 233 234 ### Examples 235 236 ```console 237 $ gomplate -i '{{ filepath.Match "*.csv" "foo.csv" }}' 238 true 239 ``` 240 241 ## `filepath.Rel` 242 243 Returns a relative path that is lexically equivalent to targetpath when joined to basepath with an intervening separator. 244 245 A wrapper for Go's [`filepath.Rel`](https://golang.org/pkg/path/filepath/#Rel) function. 246 247 ### Usage 248 249 ```go 250 filepath.Rel basepath targetpath 251 ``` 252 253 ### Arguments 254 255 | name | description | 256 |------|-------------| 257 | `basepath` | _(required)_ The base path | 258 | `targetpath` | _(required)_ The target path | 259 260 ### Examples 261 262 ```console 263 $ gomplate -i '{{ filepath.Rel "/a" "/a/b/c" }}' 264 b/c 265 ``` 266 267 ## `filepath.Split` 268 269 Splits path immediately following the final path separator, separating it into a directory and file name component. 270 271 The function returns an array with two values, the first being the diretory, and the second the file. 272 273 A wrapper for Go's [`filepath.Split`](https://golang.org/pkg/path/filepath/#Split) function. 274 275 ### Usage 276 277 ```go 278 filepath.Split path 279 ``` 280 ```go 281 path | filepath.Split 282 ``` 283 284 ### Arguments 285 286 | name | description | 287 |------|-------------| 288 | `path` | _(required)_ The input path | 289 290 ### Examples 291 292 ```console 293 $ gomplate -i '{{ $p := filepath.Split "/tmp/foo" }}{{ $dir := index $p 0 }}{{ $file := index $p 1 }}dir is {{$dir}}, file is {{$file}}' 294 dir is /tmp/, file is foo 295 C:\> gomplate.exe -i '{{ $p := filepath.Split `C:\tmp\foo` }}{{ $dir := index $p 0 }}{{ $file := index $p 1 }}dir is {{$dir}}, file is {{$file}}' 296 dir is C:\tmp\, file is foo 297 ``` 298 299 ## `filepath.ToSlash` 300 301 Returns the result of replacing each separator character in path with a slash (`/`) character. 302 303 A wrapper for Go's [`filepath.ToSlash`](https://golang.org/pkg/path/filepath/#ToSlash) function. 304 305 ### Usage 306 307 ```go 308 filepath.ToSlash path 309 ``` 310 ```go 311 path | filepath.ToSlash 312 ``` 313 314 ### Arguments 315 316 | name | description | 317 |------|-------------| 318 | `path` | _(required)_ The input path | 319 320 ### Examples 321 322 ```console 323 $ gomplate -i '{{ filepath.ToSlash "/foo/bar" }}' 324 /foo/bar 325 C:\> gomplate.exe -i '{{ filepath.ToSlash `foo\bar\baz` }}' 326 foo/bar/baz 327 ``` 328 329 ## `filepath.VolumeName` 330 331 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. 332 333 A wrapper for Go's [`filepath.VolumeName`](https://golang.org/pkg/path/filepath/#VolumeName) function. 334 335 ### Usage 336 337 ```go 338 filepath.VolumeName path 339 ``` 340 ```go 341 path | filepath.VolumeName 342 ``` 343 344 ### Arguments 345 346 | name | description | 347 |------|-------------| 348 | `path` | _(required)_ The input path | 349 350 ### Examples 351 352 ```console 353 C:\> gomplate.exe -i 'volume is {{ filepath.VolumeName "C:/foo/bar" }}' 354 volume is C: 355 $ gomplate -i 'volume is {{ filepath.VolumeName "/foo/bar" }}' 356 volume is 357 ```