when doing a WalkDir of a FileSystem, how do you get access to the path that you are working on

1,537 views
Skip to first unread message

Pat Farrell

unread,
Jan 14, 2023, 6:39:40 PM1/14/23
to golang-nuts
I'm using the reasonably new FileSystem style to do the usual directory walk
processing every file in the usual recursive manner.
I can't figure out how to get the directory data (the path up to the last /)
even though I can see it in the output.

I am missing the proper name of the getter, or perhaps a cast/type assertion


Playground:
https://go.dev/play/p/Fde-rAA5YZI

The key code is
    fsys := os.DirFS(".")
    fs.WalkDir(fsys, ".", func(p string, d fs.DirEntry, err error) error {
            fmt.Printf("%s  struct: %T  %v\n", p, d, d)

which prints "'p" the current file name and extension
the type of the fs.DirEntry and then the fsDirEntry structure

Something like this:
csvtsd/csvtsd.go  struct: *os.unixDirent  &{../csvtsd csvtsd.go 0 <nil>}
csvtsd/csvtsd_test.go  struct: *os.unixDirent  &{../csvtsd csvtsd_test.go 0 <nil>}
csvtsd/go.mod  struct: *os.unixDirent  &{../csvtsd go.mod 0 <nil>}

You can see that the "p" is typically the path and filename
csvtsd/csvtsd.go  
the type is a  *os.unixDirent
and there are four fields in the Dirent, the path (up to the last /)
the filename and extension, and two other fields.

Thanks

Raffaele Sena

unread,
Jan 14, 2023, 6:52:15 PM1/14/23
to Pat Farrell, golang-nuts
The function you implement (WalkDirFunc should receive "p" as the path to the parent  (that seems to be what you want) and "d" as the current directory entry.
I am not sure why in your example you are showing full paths to the file for "p".



--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/99c9d821-be0e-4763-ab40-72e3af61c511n%40googlegroups.com.

Pat Farrell

unread,
Jan 14, 2023, 8:24:51 PM1/14/23
to golang-nuts
On Saturday, January 14, 2023 at 6:52:15 PM UTC-5 raf wrote:
The function you implement (WalkDirFunc should receive "p" as the path to the parent  (that seems to be what you want) and "d" as the current directory entry. I am not sure why in your example you are showing full paths to the file for "p".

I don't know either. But that is what the code is doing. 

Tobias Klausmann

unread,
Jan 15, 2023, 5:50:20 AM1/15/23
to golan...@googlegroups.com
Hi!
The code is doing what the docs for WalkDirFunc[0] say:

> The path argument contains the argument to WalkDir as a prefix. That
> is, if WalkDir is called with root argument "dir" and finds a file
> named "a" in that directory, the walk function will be called with
> argument "dir/a".
>
> The d argument is the fs.DirEntry for the named path.

So `p` is always the full path to the current directory entry (i.e. it
may be a file, symlink, dir etc etc), and `d` contains a struct that
points to the same.

As I understand it, the fs-style WalkDir approach does not have a
separate argument for the directory the current path is in, nor is it
easily gleaned from the `p` or `d` arguments.

My recommendation is to use path.Dir[1] to find that information in a
portable manner.

Here's some example code:

https://go.dev/play/p/DOFzOUrVEZ6

HTH,
Tobias

[0] https://pkg.go.dev/io/f...@go1.19.5#WalkDirFunc
[1] https://pkg.go.dev/path#Dir
Reply all
Reply to author
Forward
0 new messages