-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
50 lines (42 loc) · 1.16 KB
/
example_test.go
File metadata and controls
50 lines (42 loc) · 1.16 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
48
49
50
package pathlib_test
import (
"fmt"
"github.com/skalt/pathlib.go"
)
// Syntactic sugar.
func enforce(err error) {
if err != nil {
panic(err)
}
}
// More syntactic sugar.
func expect[T any](val T, err error) T {
enforce(err)
return val
}
func Example() {
dir := expect(pathlib.TempDir().Join("pathlib-example").AsDir().Make(0o777))
defer func() { expect(dir.RemoveAll()) }()
onDisk := expect(dir.Stat())
fmt.Printf("Created %s with mode %s\n", dir, onDisk.Mode())
for i, subPath := range []string{"a.txt", "b.txt", "c/d.txt"} {
file := dir.Join(subPath).AsFile()
handle := expect(file.MakeAll(0o666, 0o777))
expect(fmt.Fprintf(handle, "%d", i))
enforce(handle.Close())
fmt.Printf("contents of %s: %q\n", file, string(expect(file.Read())))
}
fmt.Printf("contents of %s:\n", dir)
for _, entry := range expect(dir.Read()) {
fmt.Println(" - " + entry.Name())
}
// Output:
// Created /tmp/pathlib-example with mode drwxr-xr-x
// contents of /tmp/pathlib-example/a.txt: "0"
// contents of /tmp/pathlib-example/b.txt: "1"
// contents of /tmp/pathlib-example/c/d.txt: "2"
// contents of /tmp/pathlib-example:
// - a.txt
// - b.txt
// - c
}