forked from TannerGabriel/learning-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Directories.go
48 lines (46 loc) · 966 Bytes
/
Directories.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
package main
import (
"fmt"
"log"
"os"
"os/user"
"path/filepath"
)
func main() {
path := "test"
// Create an empty directory
if _, err := os.Stat(path); os.IsNotExist(err) {
mdErr := os.Mkdir(path, 0755)
if mdErr != nil {
fmt.Println("Error making directory", mdErr)
}
}
// Create file in the directory
emptyFile, err := os.Create("./test/test.txt")
if err != nil {
log.Fatal(err)
}
fmt.Println(emptyFile)
emptyFile.Close()
// Read directory recursively
filepath.Walk(path, func(fn string, fi os.FileInfo, err error) error {
if err != nil {
fmt.Println("Error while walking: ", err)
return err
}
if fi.IsDir() {
fmt.Println("Directory: ", fn)
if fi.Name() == "skipme" {
return filepath.SkipDir
}
} else {
fmt.Println("File: ", fn)
}
return nil
})
// Delete directory
os.RemoveAll("./test")
// Get users home directory
usr, err := user.Current()
fmt.Println("User Home Directory:", usr.HomeDir)
}