以高效的方式递归列出目录和文件。

huangapple go评论77阅读模式
英文:

Go listing directories and files recursively in an efficient way

问题

除了Go语言中的filepath.Walk之外,还有其他高效的方法来实现相同的功能吗?

英文:

go's filepath.Walk says that

> The files are walked in lexical order, which makes the output deterministic but means that for very large directories Walk can be inefficient. Walk does not follow symbolic links.

What are the other efficient ways to do the same?

答案1

得分: 2

没有一种“高效”的方法来做同样的事情(按词法顺序遍历)。你要么必须进行排序(这是“低效”的),要么按随机顺序遍历(这不是同样的结果)。

在你测量并且确实使用filepath.Walk是你的瓶颈之前,你不应该担心这个词“低效”。特别是因为它并没有说“它将是低效的”,只是“可能是”。可能是指:可以手动创建100k个文件名并将它们放入一个手动创建的目录中,以便对它们进行排序需要很长时间。

英文:

There is no "efficient" way to do the same (walk in lexical order). You either have to sort (which is "inefficient") or walk in random order (which is not the same).

Until you measured and using filepath.Walk really is your bottleneck you should not worry about the little word "inefficient". Especially as it does not state "it will be inefficient", just "can be". Can in the sense of: It is possible to craft 100k file names and put the into a manually crafted directory so that sorting them will take a long time.

huangapple
  • 本文由 发表于 2014年4月24日 20:27:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/23268882.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定