goexample之directories

代码源自https://github.com/mmcgrana/gobyexample

 1 // Go has several useful functions for working with
 2 // *directories* in the file system.
 3 
 4 package main
 5 
 6 import (
 7     "fmt"
 8     "io/ioutil"
 9     "os"
10     "path/filepath"
11 )
12 
13 func check(e error) {
14     if e != nil {
15         panic(e)
16     }
17 }
18 
19 func main() {
20 
21     // Create a new sub-directory in the current working
22     // directory.
23     err := os.Mkdir("subdir", 0755)
24     check(err)
25 
26     // When creating temporary directories, it's good
27     // practice to `defer` their removal. `os.RemoveAll`
28     // will delete a whole directory tree (similarly to
29     // `rm -rf`).
30     defer os.RemoveAll("subdir")
31 
32     // Helper function to create a new empty file.
33     createEmptyFile := func(name string) {
34         d := []byte("")
35         check(ioutil.WriteFile(name, d, 0644))
36     }
37 
38     createEmptyFile("subdir/file1")
39 
40     // We can create a hierarchy of directories, including
41     // parents with `MkdirAll`. This is similar to the
42     // command-line `mkdir -p`.
43     err = os.MkdirAll("subdir/parent/child", 0755)
44     check(err)
45 
46     createEmptyFile("subdir/parent/file2")
47     createEmptyFile("subdir/parent/file3")
48     createEmptyFile("subdir/parent/child/file4")
49 
50     // `ReadDir` lists directory contents, returning a
51     // slice of `os.FileInfo` objects.
52     c, err := ioutil.ReadDir("subdir/parent")
53     check(err)
54 
55     fmt.Println("Listing subdir/parent")
56     for _, entry := range c {
57         fmt.Println(" ", entry.Name(), entry.IsDir())
58     }
59 
60     // `Chdir` lets us change the current working directory,
61     // similarly to `cd`.
62     err = os.Chdir("subdir/parent/child")
63     check(err)
64 
65     // Now we'll see the contents of `subdir/parent/child`
66     // when listing the *current* directory.
67     c, err = ioutil.ReadDir(".")
68     check(err)
69 
70     fmt.Println("Listing subdir/parent/child")
71     for _, entry := range c {
72         fmt.Println(" ", entry.Name(), entry.IsDir())
73     }
74 
75     // `cd` back to where we started.
76     err = os.Chdir("../../..")
77     check(err)
78 
79     // We can also visit a directory *recursively*,
80     // including all its sub-directories. `Walk` accepts
81     // a callback function to handle every file or
82     // directory visited.
83     fmt.Println("Visiting subdir")
84     err = filepath.Walk("subdir", visit)
85 }
86 
87 // `visit` is called for every file or directory found
88 // recursively by `filepath.Walk`.
89 func visit(p string, info os.FileInfo, err error) error {
90     if err != nil {
91         return err
92     }
93     fmt.Println(" ", p, info.IsDir())
94     return nil
95 }

 os.Mkdir  

 1 // Mkdir creates a new directory with the specified name and permission
 2 // bits (before umask).
 3 // If there is an error, it will be of type *PathError.
 4 func Mkdir(name string, perm FileMode) error {
 5     if runtime.GOOS == "windows" && isWindowsNulName(name) {
 6         return &PathError{"mkdir", name, syscall.ENOTDIR}
 7     }
 8     e := syscall.Mkdir(fixLongPath(name), syscallMode(perm))
 9 
10     if e != nil {
11         return &PathError{"mkdir", name, e}
12     }
13 
14     // mkdir(2) itself won't handle the sticky bit on *BSD and Solaris
15     if !supportsCreateWithStickyBit && perm&ModeSticky != 0 {
16         e = setStickyBit(name)
17 
18         if e != nil {
19             Remove(name)
20             return e
21         }
22     }
23 
24     return nil
25 }

待续。。。。

猜你喜欢

转载自www.cnblogs.com/chenguifeng/p/11839414.html