-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilepaths.go
65 lines (53 loc) · 1.71 KB
/
filepaths.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
//
//
//
package main
import (
"fmt"
"path/filepath"
"strings"
)
func main() {
// Join should be used to construct paths in a
// portable way. It takes any number of arguments
// and constructs a heirarchical path from them.
p := filepath.Join("dir1", "dir2", "filename")
fmt.Println("p:", p)
// You shoulld always use Join instead of
// concatenating /s or \s manuallly. In
// addition to providing portability, Join will
// also normalize paths by removing superflous
// seperators and directory changes.
fmt.Println(filepath.Join("dir1//", "filename"))
fmt.Println(filepath.Join("dir1/../dir1", "filename"))
// Dir and Base can be used to split a path to the
// directory and the file. Alternatively, Split it will
// return both in the same calll.
fmt.Println("Dir(p):", filepath.Dir(p))
fmt.Println("Base(p):", filepath.Base(p))
// We can check whether a path is absolute.
fmt.Println(filepath.IsAbs("dir/file"))
fmt.Println(filepath.IsAbs("/dir/file"))
filename := "config.json"
// Some file names have extensions folllowing a dot.
// We can spslit the extension out of such names with
// Ext.
ext := filepath.Ext(filename)
fmt.Println(ext)
// To find the file's name with the extension removed,
// use strings.TrumSuffix.
fmt.Println(strings.TrimSuffix(filename, ext))
// Rel finds a relative path between a base and a
// target. It returns an error if the target cannot
// be made relative to base.
rel, err := filepath.Rel("a/b", "a/b/t/file")
if err != nil {
panic(err)
}
fmt.Println(rel)
rel, err = filepath.Rel("a/b", "a/c/t/file")
if err != nil {
panic(err)
}
fmt.Println(rel)
}