Search for directory named BigZoom and list contents.

64 views
Skip to first unread message

Adam C

unread,
May 9, 2021, 10:53:20 PM5/9/21
to golang-nuts

I'm trying to come up with a fairly small script to find a directory and then list the contents of a given directory name.

The filestructure is thus:
/root/1/1
/root/1/2
/root/1/BigZoom
/root/1/BigZoom/1
/root/2/1
/root/2/2
/root/2/BigZoom
/root/2/BigZoom/1

I want to find any file in a directory named BigZoom. I'm new to Go, but thought I'd give it a go.

I use filepath.WalkDir in this code https://play.golang.org/p/ivqA7o2nCCs - it seems that my problem is passing a fileinfo.Name to ReadDir. What should go on line 25 for the ReadDir call? I thought I could pass in a string, but I'm getting:

panic: open BigZoom: no such file or directory

Any help would be much appreciated - thanks in advance.

Adam

Martin Schnabel

unread,
May 10, 2021, 6:19:24 AM5/10/21
to Adam C, golang-nuts
hi Adam,

you call ReadDir with the file name and not with the full path or the
path relative to your current working directory. that means you will try
to read the dir with path "BigZoom" two times. the WalkDirFunc passes a
path argument that concatenates the path prefix with the dir entry name
to form the whole path you can use to read the file.

https://play.golang.org/p/76GxS3TRkha

not locally tested with files, but i hope it shows what i mean.

have fun
> --
> 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
> <mailto:golang-nuts...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/golang-nuts/9ec35241-8fa6-49bd-ad07-56b57cc8cd3an%40googlegroups.com
> <https://groups.google.com/d/msgid/golang-nuts/9ec35241-8fa6-49bd-ad07-56b57cc8cd3an%40googlegroups.com?utm_medium=email&utm_source=footer>.

Adam

unread,
May 10, 2021, 8:09:52 AM5/10/21
to golang-nuts
Hi Kurt - many thanks. I got it - I switched it to pass in the var 'p' instead of info.Name() and it works.
Adam

On Mon, 10 May 2021 at 04:19, Kurtis Rader <kra...@skepticism.us> wrote:
Hint #1: Note that fs.DirEntry.Name() only contains the final component of the path: https://golang.org/pkg/io/fs/#DirEntry.

Hint #2: Note that fs.WalkDirFunc()'s first argument is the full path to the directory entry: https://golang.org/pkg/io/fs/#WalkDirFunc. See the example for the older fs.Walk() function: https://golang.org/pkg/path/filepath/#example_Walk.

--
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/9ec35241-8fa6-49bd-ad07-56b57cc8cd3an%40googlegroups.com.


--
Kurtis Rader
Caretaker of the exceptional canines Junior and Hank

Kurtis Rader

unread,
May 10, 2021, 8:09:53 AM5/10/21
to Adam C, golang-nuts
Hint #1: Note that fs.DirEntry.Name() only contains the final component of the path: https://golang.org/pkg/io/fs/#DirEntry.

Hint #2: Note that fs.WalkDirFunc()'s first argument is the full path to the directory entry: https://golang.org/pkg/io/fs/#WalkDirFunc. See the example for the older fs.Walk() function: https://golang.org/pkg/path/filepath/#example_Walk.

On Sun, May 9, 2021 at 7:53 PM Adam C <ad...@monkeez.org> wrote:
--
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/9ec35241-8fa6-49bd-ad07-56b57cc8cd3an%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages