Re: [go-nuts] when doing a WalkDir of a FileSystem, how do you get access to the path that you are working on

2023-01-15 Thread Tobias Klausmann
Hi! 

On Sat, 14 Jan 2023, Pat Farrell wrote:
> 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. 

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/fs@go1.19.5#WalkDirFunc
[1] https://pkg.go.dev/path#Dir

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/Y8PaTSznSML8wXYa%40skade.schwarzvogel.de.


Re: [go-nuts] when doing a WalkDir of a FileSystem, how do you get access to the path that you are working on

2023-01-14 Thread Pat Farrell


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. 

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/4bdfb9e5-be8c-407b-9885-f476e4c7bff2n%40googlegroups.com.


Re: [go-nuts] when doing a WalkDir of a FileSystem, how do you get access to the path that you are working on

2023-01-14 Thread Raffaele Sena
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".



On Sat, Jan 14, 2023 at 3:39 PM Pat Farrell  wrote:

> 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 }
> csvtsd/csvtsd_test.go  struct: *os.unixDirent  &{../csvtsd csvtsd_test.go
> 0 }
> csvtsd/go.mod  struct: *os.unixDirent  &{../csvtsd go.mod 0 }
>
> 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
>
> --
> 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+unsubscr...@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
> 
> .
>

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CANKfucZx3QZdJ4heXEXLO6LuB5eE71UwQg3rYNH6BiQzhrvgvg%40mail.gmail.com.


[go-nuts] when doing a WalkDir of a FileSystem, how do you get access to the path that you are working on

2023-01-14 Thread Pat Farrell
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 }
csvtsd/csvtsd_test.go  struct: *os.unixDirent  &{../csvtsd csvtsd_test.go 0 
}
csvtsd/go.mod  struct: *os.unixDirent  &{../csvtsd go.mod 0 }

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

-- 
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+unsubscr...@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.