-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemp_test.go
More file actions
75 lines (63 loc) · 1.38 KB
/
Copy pathtemp_test.go
File metadata and controls
75 lines (63 loc) · 1.38 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package fs_test
import (
"context"
"fmt"
"log"
"lesiw.io/fs"
"lesiw.io/fs/osfs"
)
func ExampleTemp_dir() {
fsys, ctx := osfs.NewTemp(), context.Background()
defer fs.Close(fsys)
// Create temp directory (trailing slash indicates directory)
w, err := fs.Temp(ctx, fsys, "myapp/")
if err != nil {
log.Fatal(err)
}
defer w.Close()
// Get the directory path and defer cleanup
dir := w.Path()
defer fs.RemoveAll(ctx, fsys, dir)
// Create a file in the temp directory
err = fs.WriteFile(ctx, fsys, dir+"/data.txt", []byte("temporary data"))
if err != nil {
log.Fatal(err)
}
// Read it back
data, err := fs.ReadFile(ctx, fsys, dir+"/data.txt")
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s\n", data)
// Output:
// temporary data
}
func ExampleTemp_file() {
fsys, ctx := osfs.NewTemp(), context.Background()
defer fs.Close(fsys)
// Create temp file (no trailing slash)
w, err := fs.Temp(ctx, fsys, "myapp")
if err != nil {
log.Fatal(err)
}
// Get the file path and defer cleanup
path := w.Path()
defer fs.Remove(ctx, fsys, path)
// Write to the temp file
_, err = w.Write([]byte("temporary data"))
if err != nil {
_ = w.Close()
log.Fatal(err)
}
if err := w.Close(); err != nil {
log.Fatal(err)
}
// Read it back
data, err := fs.ReadFile(ctx, fsys, path)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s\n", data)
// Output:
// temporary data
}