3 ms·
Assuming latest Go 1.13 I would write an iterator and used goroutines internally. The caller would do: for f := range asyncDirIter(dir) { } Better th
by kjksf 2y ago
Assuming latest Go 1.13 I would write an iterator and used goroutines internally.
The caller would do:
for f := range asyncDirIter(dir) {
}
Better than exposing channel.
But my first question would be: is it really necessary? Are you really scanning such large directories to make async dir traversal beneficial?
I actually did that once but that that was for a program that scanned the whole drive. I wouldn't do it for scanning a local drive with 100 files.
Finally, you re-defined "traversing a tree" into "traversing a filesystem".
I assume that the post you're responding to was talking about traversing a tree structure in memory. In that context using goroutines is an overkill. Harder to implement, harder to use and slower.
- lelanthran 2y ago> In that context using goroutines is an overkill. Harder to implement, harder to use and slower. I agree with this, but my assumption was very different to yours: that the tree was sufficiently large and/or the processing was sufficiently long to make the caller wait unreasonably long while walking the tree. For scanning < 1000 files on the local filesystem, I'd probably just scan it and return a list populated by a predicate function. For even 20 files on a network filesystem, I'd make it async.