3 回答
TA貢獻1811條經驗 獲得超5個贊
您需要使用文件路徑。WalkDir 以遞歸方式檢查目錄,也可以使用 1.16 文件路徑中引入的目錄。步行。 將僅在提供的目錄中工作。os.ReadDir
filepath.Walk(".", func(path string, info fs.FileInfo, err error) error {
if err != nil {
fmt.Printf("prevent panic by handling failure accessing a path %q: %v\n", path, err)
return err
}
if strings.HasSuffix(info.Name(), ".md") {
fmt.Printf("visited file or dir: %q\n", path)
}
return nil
})
TA貢獻1812條經驗 獲得超5個贊
為了添加已經足夠的響應,這是我使用結構實現的container/ring
完整代碼在這里
type (
DirectoryGraph struct {
RootPath string
root *ring.Ring
Node *ring.Ring
}
)
func NewDirectoryGraph(root string) DirectoryGraph {
r := ring.New(1)
graph := DirectoryGraph{
RootPath: root,
root: r,
Node: r,
}
filepath.WalkDir(graph.RootPath, graph.walk)
return graph
}
func (g DirectoryGraph) walk(s string, d fs.DirEntry, e error) error {
if e != nil {
return e
}
next := ring.New(1)
node := g.serialize(s, d, e)
next.Value = node
g.root.Link(next).Next()
return nil
}
// Serializes a file-node
func (g DirectoryGraph) serialize(s string, d fs.DirEntry, e error) FileNode {
n := FileNode{
Path: s,
Dir: d,
Sys: SysInfo{},
}
...
return n
}
- 3 回答
- 0 關注
- 149 瀏覽
添加回答
舉報
