]> Cypherpunks repositories - gostls13.git/commitdiff
doc: provide example filepath.Walk for go1
authorMike Rosset <mike.rosset@gmail.com>
Fri, 17 Feb 2012 01:45:55 +0000 (12:45 +1100)
committerRob Pike <r@golang.org>
Fri, 17 Feb 2012 01:45:55 +0000 (12:45 +1100)
R=golang-dev, r, r
CC=golang-dev
https://golang.org/cl/5674067

doc/go1.html
doc/go1.tmpl
doc/progs/go1.go

index 13d74012bcdb3473eeb7252d152febed9c356ea1..60f71075e331ad28ce886650e1409bbeb12a4b11 100644 (file)
@@ -1540,12 +1540,24 @@ instead of a <code>Visitor</code> interface value.
 The <code>WalkFunc</code> 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 <code>SkipDir</code>.
+the function should return the value <a href="/pkg/path/filepath/#variables"><code>filepath.SkipDir</code></a>
 </p>
 
-<p>
-<font color="red">TODO: add an example?</font>
-</p>
+<pre><!--{{code "progs/go1.go" `/STARTWALK/` `/ENDWALK/`}}
+-->    markFn := func(path string, info os.FileInfo, err error) error {
+        if path == &#34;pictures&#34; { // 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(&#34;.&#34;, markFn)
+    if err != nil {
+        log.Fatal(err)
+    }</pre>
 
 <p>
 <em>Updating</em>:
index a963b14984673f4335c61b033cb98ff46ea68b40..c31fa7f2cf24afa6c7ffe8f67f30acec534382b1 100644 (file)
@@ -1439,12 +1439,10 @@ instead of a <code>Visitor</code> interface value.
 The <code>WalkFunc</code> 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 <code>SkipDir</code>.
+the function should return the value <a href="/pkg/path/filepath/#variables"><code>filepath.SkipDir</code></a>
 </p>
 
-<p>
-<font color="red">TODO: add an example?</font>
-</p>
+{{code "progs/go1.go" `/STARTWALK/` `/ENDWALK/`}}
 
 <p>
 <em>Updating</em>:
index 653c97fbf5a5d6dd084f15b6b6b7f9ccb4b264cf..1507d5b33b54c9a1a447189bbd7c45cd60de675f 100644 (file)
@@ -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
 }