Skip to content

Commit

Permalink
doc: provide example filepath.Walk for go1
Browse files Browse the repository at this point in the history
R=golang-dev, r, r
CC=golang-dev
https://golang.org/cl/5674067
  • Loading branch information
mrosset authored and robpike committed Feb 17, 2012
1 parent 785ee50 commit 9167268
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 8 deletions.
20 changes: 16 additions & 4 deletions doc/go1.html
Original file line number Diff line number Diff line change
Expand Up @@ -1540,12 +1540,24 @@ <h3 id="path_filepath">The path/filepath package</h3>
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>:
Expand Down
6 changes: 2 additions & 4 deletions doc/go1.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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>:
Expand Down
21 changes: 21 additions & 0 deletions doc/progs/go1.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"fmt"
"log"
"os"
"path/filepath"
"testing"
"time"
"unicode"
Expand All @@ -28,6 +29,7 @@ func main() {
runeType()
errorExample()
timePackage()
walkExample()
osIsExist()
}

Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 9167268

Please sign in to comment.