From 91672686da0af0d2e21b022c36b9977a78ec490f Mon Sep 17 00:00:00 2001 From: Mike Rosset Date: Fri, 17 Feb 2012 12:45:55 +1100 Subject: [PATCH] doc: provide example filepath.Walk for go1 R=golang-dev, r, r CC=golang-dev https://golang.org/cl/5674067 --- doc/go1.html | 20 ++++++++++++++++---- doc/go1.tmpl | 6 ++---- doc/progs/go1.go | 21 +++++++++++++++++++++ 3 files changed, 39 insertions(+), 8 deletions(-) diff --git a/doc/go1.html b/doc/go1.html index 13d74012bcd..60f71075e33 100644 --- a/doc/go1.html +++ b/doc/go1.html @@ -1540,12 +1540,24 @@ instead of a Visitor interface value. The WalkFunc function will be called even for files or directories that could not be opened; in such cases the error argument will describe the failure. If a directory's contents are to be skipped, -the function should return the value SkipDir. +the function should return the value filepath.SkipDir

-

-TODO: add an example? -

+
    markFn := func(path string, info os.FileInfo, err error) error {
+        if path == "pictures" { // Will skip walking of directory pictures and its contents.
+            return filepath.SkipDir
+        }
+        if err != nil {
+            return err
+        }
+        log.Println(path)
+        return nil
+    }
+    err := filepath.Walk(".", markFn)
+    if err != nil {
+        log.Fatal(err)
+    }

Updating: diff --git a/doc/go1.tmpl b/doc/go1.tmpl index a963b149846..c31fa7f2cf2 100644 --- a/doc/go1.tmpl +++ b/doc/go1.tmpl @@ -1439,12 +1439,10 @@ instead of a Visitor interface value. The WalkFunc function will be called even for files or directories that could not be opened; in such cases the error argument will describe the failure. If a directory's contents are to be skipped, -the function should return the value SkipDir. +the function should return the value filepath.SkipDir

-

-TODO: add an example? -

+{{code "progs/go1.go" `/STARTWALK/` `/ENDWALK/`}}

Updating: diff --git a/doc/progs/go1.go b/doc/progs/go1.go index 653c97fbf5a..1507d5b33b5 100644 --- a/doc/progs/go1.go +++ b/doc/progs/go1.go @@ -12,6 +12,7 @@ import ( "fmt" "log" "os" + "path/filepath" "testing" "time" "unicode" @@ -28,6 +29,7 @@ func main() { runeType() errorExample() timePackage() + walkExample() osIsExist() } @@ -183,6 +185,25 @@ func timePackage() { sleepUntil(time.Now().Add(123 * time.Millisecond)) } +func walkExample() { + // STARTWALK OMIT + markFn := func(path string, info os.FileInfo, err error) error { + if path == "pictures" { // Will skip walking of directory pictures and its contents. + return filepath.SkipDir + } + if err != nil { + return err + } + log.Println(path) + return nil + } + err := filepath.Walk(".", markFn) + if err != nil { + log.Fatal(err) + } + // ENDWALK OMIT +} + func initializationFunction(c chan int) { c <- 1 }