From 1be2c1c9100d53294505f42e9d8e468a4cbe7fce Mon Sep 17 00:00:00 2001 From: Eli Bendersky Date: Sat, 8 Jun 2019 11:20:00 -0700 Subject: [PATCH] Add Walk --- examples/directories/directories.go | 22 ++++++++- examples/directories/directories.hash | 4 +- examples/directories/directories.sh | 16 +++++-- public/directories | 64 +++++++++++++++++++++++---- 4 files changed, 89 insertions(+), 17 deletions(-) diff --git a/examples/directories/directories.go b/examples/directories/directories.go index 0384354..8abdb51 100644 --- a/examples/directories/directories.go +++ b/examples/directories/directories.go @@ -7,6 +7,7 @@ import ( "fmt" "io/ioutil" "os" + "path/filepath" ) func check(e error) { @@ -53,7 +54,7 @@ func main() { fmt.Println("Listing subdir/parent") for _, entry := range c { - fmt.Println(entry.Name(), entry.IsDir()) + fmt.Println(" ", entry.Name(), entry.IsDir()) } // `Chdir` lets us change the current working directory, @@ -68,10 +69,27 @@ func main() { fmt.Println("Listing subdir/parent/child") for _, entry := range c { - fmt.Println(entry.Name(), entry.IsDir()) + fmt.Println(" ", entry.Name(), entry.IsDir()) } // `cd` back to where we started. err = os.Chdir("../../..") check(err) + + // We can also visit a directory *recursively*, + // including all its sub-directories. `Walk` accepts + // a callback function to handle every file or + // directory visited. + fmt.Println("Visiting subdir") + err = filepath.Walk("subdir", visit) +} + +// visit is called for every file or directory found +// recursively by `filepath.Walk`. +func visit(p string, info os.FileInfo, err error) error { + if err != nil { + return err + } + fmt.Println(" ", p, info.IsDir()) + return nil } diff --git a/examples/directories/directories.hash b/examples/directories/directories.hash index b599edd..75cb9fa 100644 --- a/examples/directories/directories.hash +++ b/examples/directories/directories.hash @@ -1,2 +1,2 @@ -7b2eed223a00c8a84df582bd254a642c7a57dd9b -UnjBL6NmR8- +57a8f629f040270c15a0375be4424b392edf3c95 +LI7ty_KDozd diff --git a/examples/directories/directories.sh b/examples/directories/directories.sh index cc48f74..31a8f0c 100644 --- a/examples/directories/directories.sh +++ b/examples/directories/directories.sh @@ -1,7 +1,15 @@ $ go run directories.go Listing subdir/parent -child true -file2 false -file3 false + child true + file2 false + file3 false Listing subdir/parent/child -file4 false + file4 false +Visiting subdir + subdir true + subdir/file1 false + subdir/parent true + subdir/parent/child true + subdir/parent/child/file4 false + subdir/parent/file2 false + subdir/parent/file3 false diff --git a/public/directories b/public/directories index f80b48f..a1bfce5 100644 --- a/public/directories +++ b/public/directories @@ -28,7 +28,7 @@ - +
package main
@@ -47,6 +47,7 @@
     "fmt"
     "io/ioutil"
     "os"
+    "path/filepath"
 )
 
@@ -143,7 +144,7 @@ will delete a whole directory tree (similarly to

We can create a hierarchy of directories, including -parents wiht MkdirAll. This is similar to the +parents with MkdirAll. This is similar to the command-line mkdir -p.

@@ -193,7 +194,7 @@ slice of os.FileInfo objects.

    fmt.Println("Listing subdir/parent")
     for _, entry := range c {
-        fmt.Println(entry.Name(), entry.IsDir())
+        fmt.Println(" ", entry.Name(), entry.IsDir())
     }
 
@@ -238,7 +239,7 @@ when listing the current directory.

    fmt.Println("Listing subdir/parent/child")
     for _, entry := range c {
-        fmt.Println(entry.Name(), entry.IsDir())
+        fmt.Println(" ", entry.Name(), entry.IsDir())
     }
 
@@ -250,10 +251,47 @@ when listing the current directory.

cd back to where we started.

- +
    err = os.Chdir("../../..")
     check(err)
+
+ + + + + + +

We can also visit a directory recursively, +including all its sub-directories. Walk accepts +a callback function to handle every file or +directory visited.

+ + + + +
    fmt.Println("Visiting subdir")
+    err = filepath.Walk("subdir", visit)
+}
+
+ + + + + + +

visit is called for every file or directory found +recursively by filepath.Walk.

+ + + + +
func visit(p string, info os.FileInfo, err error) error {
+    if err != nil {
+        return err
+    }
+    fmt.Println(" ", p, info.IsDir())
+    return nil
 }
 
@@ -272,11 +310,19 @@ when listing the current directory.

$ go run directories.go
 Listing subdir/parent
-child true
-file2 false
-file3 false
+  child true
+  file2 false
+  file3 false
 Listing subdir/parent/child
-file4 false
+  file4 false
+Visiting subdir
+  subdir true
+  subdir/file1 false
+  subdir/parent true
+  subdir/parent/child true
+  subdir/parent/child/file4 false
+  subdir/parent/file2 false
+  subdir/parent/file3 false