-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
64 lines (54 loc) · 1.46 KB
/
main.go
File metadata and controls
64 lines (54 loc) · 1.46 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
// The `so/os` package provides basic functions
// for working with the file system.
package main
import (
"solod.dev/so/fmt"
"solod.dev/so/mem"
"solod.dev/so/os"
)
func main() {
// Create a new sub-directory in the current working directory.
const dirname = "subdir"
err := os.Mkdir(dirname, 0755)
check(err)
// When creating temporary directories, it's good
// practice to defer their removal.
defer removeAll(dirname)
createFile(dirname + "/file1")
createFile(dirname + "/file2")
createFile(dirname + "/file3")
// ReadDir lists directory contents, returning a
// slice of os.DirEntry objects.
entries, err := os.ReadDir(mem.System, dirname)
check(err)
defer os.FreeDirEntry(mem.System, entries)
fmt.Println("Listing", dirname)
for _, entry := range entries {
fmt.Println("-", entry.Name)
}
// Change the current working directory with Chdir.
err = os.Chdir("subdir")
check(err)
err = os.Chdir("..")
check(err) // back to the original directory
}
// Helper function to create a new empty file.
func createFile(fpath string) {
data := []byte("")
err := os.WriteFile(fpath, data, 0644)
check(err)
}
func removeAll(dirname string) {
// os.Remove deletes a file or an empty directory.
os.Remove(dirname + "/file1")
os.Remove(dirname + "/file2")
os.Remove(dirname + "/file3")
os.Remove(dirname)
}
// check panics on error, to make examples more concise.
// Don't do this in production code.
func check(err error) {
if err != nil {
panic(err)
}
}