-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReadDirFile.go
98 lines (80 loc) · 2.12 KB
/
ReadDirFile.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package main
import (
"fmt"
"io/fs"
"os"
)
type customReadDirFile struct {
file *os.File
}
func (f *customReadDirFile) ReadDir(n int) ([]fs.DirEntry, error) {
fmt.Println("using custom ReadDir...")
return f.file.ReadDir(n)
}
func (f *customReadDirFile) Stat() (fs.FileInfo, error) {
return f.file.Stat()
}
func (f *customReadDirFile) Read(data []byte) (int, error) {
return f.file.Read(data)
}
func (f *customReadDirFile) Close() error {
return f.file.Close()
}
func main() {
file, err := os.Open("dir")
if err != nil {
fmt.Println("发生错误:", err)
return
}
customRdf := &customReadDirFile{file: file}
var fsrdf fs.ReadDirFile
fsrdf = customRdf
_, ok := fsrdf.(fs.ReadDirFile)
fmt.Println("customReadDirFile类型是否实现了fs.ReadDirFile?", ok)
fmt.Println("1 ------------------------------------")
entries, err := customRdf.ReadDir(-1)
if err != nil {
fmt.Println("发生错误:", err)
return
}
fmt.Println("读取到的列表:")
for _, entry := range entries {
fmt.Println(entry.Name())
}
fmt.Println("2 ------------------------------------")
entries, err = customRdf.ReadDir(-1)
if err != nil {
fmt.Println(err)
return
}
fmt.Println("读取到的列表:")
for _, entry := range entries {
fmt.Println(entry.Name())
}
fmt.Println("3 ------------------------------------")
entries, err = customRdf.ReadDir(1)
fmt.Println("若想每次调用 ReadDir 都有值,则需要重新打开对应目录。例如这里的 dir目录!")
if err != nil {
fmt.Println("发生错误:", err)
return
}
fmt.Println("读取到的列表:")
for _, entry := range entries {
fmt.Println(entry.Name())
}
}
// Output:
//customReadDirFile类型是否实现了fs.ReadDirFile? true
//1 ------------------------------------
//using custom ReadDir...
//读取到的列表:
//0.txt
//subdir1
//subdir2
//2 ------------------------------------
//using custom ReadDir...
//读取到的列表:
//3 ------------------------------------
//using custom ReadDir...
//若想每次调用 ReadDir 都有值,则需要重新打开对应目录。例如这里的 dir目录!
//发生错误: EOF