Lets say I have the following directory structure:
RootDir
---SubDir1
------SubSubDir
---------file1
---------file2
---SubDir2
---SubDir3
---file3
---file4
I want to iterate only over the contents of the RootDir(SubDir1, Subdir2, Subdir3, file3, file 4)
and check if it is a dir or a file, without entering into the subdirectories, like filepath.Walk
does.
Is there any way to do this in the Go
library ?
edit:
files, err := os.Open("c:\\Documents")
file, err := files.Readdir(0)
if err != nil {
fmt.Printf("Error: %s
", err)
}
for f := range file {
fmt.Println(f.IsDir())
}
So here I am trying to iterate trough the FileInfo, which is a slice, and check for every file if it is a directory, but I always get this error:
f.IsDir undefined (type int has no field or method IsDir)
You can read a directory by opening it with os.Open
and the returned *os.File
has a Readdir()
method which gives os.FileInfo
s for the direct folder content and these FileInfo
have an IsDir()
method.