-
Notifications
You must be signed in to change notification settings - Fork 0
/
osUse.go
61 lines (51 loc) · 1.12 KB
/
osUse.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
49
50
51
52
53
54
55
56
57
58
59
60
61
package main
import (
"fmt"
"os"
"path"
"syscall"
)
func main() {
//List("/tmp/tmp.txt")
Name()
}
func Name() {
p := "/tmp/tmp.txt"
fp, err := os.OpenFile(p, os.O_WRONLY|os.O_CREATE, 0666)
fmt.Println(err)
fmt.Println(fp.Name())
}
func List(filepath string) ([]string, error) {
dir, err := os.Open(filepath)
fmt.Println("open", dir, err)
if err != nil {
fmt.Println(syscall.ENOENT == err)
if os.IsNotExist(err) {
fmt.Println("not exist", dir, err)
return nil, os.ErrNotExist
}
return nil, err
}
defer dir.Close()
fileNames, err := dir.Readdirnames(0)
fmt.Println("Readdirnames", fileNames, err)
if err != nil {
return nil, err
}
keys := make([]string, 0, len(fileNames))
for _, fileName := range fileNames {
keys = append(keys, path.Join(filepath, fileName))
}
fmt.Println(keys)
return keys, nil
}
func Stat() {
fi, err := os.Stat("/tmp")
fmt.Printf("err = %+v\n", err)
fmt.Printf("fi = %+v\n", fi)
fmt.Printf("fi.IsDir() = %+v\n", fi.IsDir())
fi, err = os.Stat("/tmp/tmp.txt")
fmt.Printf("err = %+v\n", err)
fmt.Printf("fi = %+v\n", fi)
fmt.Printf("fi.IsDir() = %+v\n", fi.IsDir())
}