On 19 September 2013 21:25, Keith Rarick <
k...@xph.us> wrote:
> On Thu, Sep 19, 2013 at 7:13 AM, roger peppe <
rogp...@gmail.com> wrote:
>> One thought: a few times I've wanted to truncate the walk at the
>> current directory
>> *after* I've seen the entry for the current directory;
>> usually when searching for directories with particular contents.
>> I'm wondering if it might be nice if SkipDir was renamed to Skip
>> (a no-op if the current file is not a directory) and SkipDir was
>> changed to mean "skip past all other files in the current directory".
>
> Ahhh interesting. I haven't run across this need myself,
> but it's the sort of thing that seems annoyingly awkward
> to do for the user and pretty straightforward to implement
> inside the walker. Not sure about the names. I'd sort of
> be inclined to keep the current method and add SkipRest.
>
>> Something I'm not sure about is that if you've got a directory
>> that you can't read, you see the name twice in a row.
>> I think I'd prefer it if it always tried to read the directory, even
>> if ends up being skipped, to preserve the nice invariant that
>> you see each name at most once.
>
> Yeah, filepath.Walk does the same thing. It struck me as
> a little weird, but I tried to stick to its behavior as closely
> as possible.
>
> I don't feel strongly about it. Do others have an opinion?
> Is this worth changing?
It does seem weird to me and I'd like to see a more obvious behaviour,
but that does have its down-sides (it uses more resources if the
client is going to skip a directory).
It would be nice if:
w := fs.Walk(dir)
var names []string
for w.Step() {
names = append(names, w.Path())
}
was guaranteed to produce a list of all stat-able names
in the directory. Quite often you don't really care about
names in inaccessible directories, and it feels like the current
behaviour could give some quite surprising results in
obscure situations.
Personally I've never relied on that particular filepath
behaviour, but others' mileage may vary.
One other thing that feels slightly awkward to me is
that Stat may return nil if the root directory does not
exist. This is underdocumented and leads to slightly
more awkward code to make things right.
At the cost of slightly more client code, I wonder about:
// Walk returns a new Walker rooted at root.
// It returns an error if the given root is not a directory.
func Walk(root string) (*Walker, error)
Then you can write code that always assumes a valid
os.FileInfo inside the loop, which is worth a fair amount.
>> One final thing: in these kinds of iterators, I've often seen
>> "Next" used instead of "Step". Worth considering, perhaps.
>
> True, and I vacillated between those names for a while.
> Figured Step was more perambulatorily thematic.
"Next" works well to imply the "next step" too, I think.
It would be nice not to invent too many names here,
and it's actually potentially usable as an interface,
though I can't currently think of a good use case.
It's a pity that Scanner chose Scan, not Next IMO.