Listing a directory
This recipe will show you how to list directory content.
How to do it...
- Open the console and create the folder
chapter06/recipe05
. - Navigate to the directory.
- Create a directory named
folder
. - Create the
listdir.go
file with the following content:
package main import ( "fmt" "io/ioutil" "os" "path/filepath" ) func main() { fmt.Println("List by ReadDir") listDirByReadDir(".") fmt.Println() fmt.Println("List by Walk") listDirByWalk(".") } func listDirByWalk(path string) { filepath.Walk(path, func(wPath string, info os.FileInfo, err error) error { // Walk the given dir // without printing out. if wPath == path { return nil } // If given path is folder // stop list recursively and print as folder. if info.IsDir(...