-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
47 lines (39 loc) · 1.22 KB
/
main.go
File metadata and controls
47 lines (39 loc) · 1.22 KB
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
// The `so/path` package provides functions
// to parse and construct Unix-like paths.
package main
import (
"solod.dev/so/fmt"
"solod.dev/so/mem"
"solod.dev/so/path"
"solod.dev/so/strings"
)
func main() {
// Join takes any number of arguments and constructs
// a hierarchical path from them.
p := path.Join(mem.System, "dir1", "dir2", "filename")
fmt.Println(p)
mem.FreeString(mem.System, p)
// Join also normalizes paths by removing extra separators and directory changes.
p = path.Join(mem.System, "dir1//", "filename")
fmt.Println(p)
mem.FreeString(mem.System, p)
p = path.Join(mem.System, "dir1/../dir1", "filename")
fmt.Println(p)
mem.FreeString(mem.System, p)
const fpath = "path/to/file.txt"
println(fpath)
// Dir and Base split a path into the directory and the file.
// Alternatively, Split returns both at once.
dir := path.Dir(mem.System, fpath)
fmt.Println("- dir =", dir)
mem.FreeString(mem.System, dir)
base := path.Base(fpath)
fmt.Println("- base =", base)
// Use Ext to get the file extension.
ext := path.Ext(fpath)
fmt.Println("- ext =", ext)
// To find the file's name with the extension removed,
// use strings.TrimSuffix.
stem := strings.TrimSuffix(base, ext)
fmt.Println("- stem =", stem)
}