英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论