From: Jean-Francois Cantin Date: Tue, 26 Sep 2017 22:07:27 +0000 (-0600) Subject: path/filepath: add example for Walk X-Git-Tag: go1.10beta1~959 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=97590aea672d2fccffc28162eddda19ec04fa328;p=gostls13.git path/filepath: add example for Walk Fixes: #22052 Change-Id: Ia056871b35ecc1a8c5ac891402fc1c5702731623 Reviewed-on: https://go-review.googlesource.com/66330 Run-TryBot: Ian Lance Taylor TryBot-Result: Gobot Gobot Reviewed-by: Ian Lance Taylor --- diff --git a/src/path/filepath/example_unix_test.go b/src/path/filepath/example_unix_test.go index cd8233ceb6..40bc547fe4 100644 --- a/src/path/filepath/example_unix_test.go +++ b/src/path/filepath/example_unix_test.go @@ -8,6 +8,7 @@ package filepath_test import ( "fmt" + "os" "path/filepath" ) @@ -79,3 +80,24 @@ func ExampleJoin() { // a/b/c // a/b/c } +func ExampleWalk() { + dir := "dir/to/walk" + subDirToSkip := "skip" // dir/to/walk/skip + + err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error { + if err != nil { + fmt.Printf("prevent panic by handling failure accessing a path %q: %v\n", dir, err) + return err + } + if info.IsDir() && info.Name() == subDirToSkip { + fmt.Printf("skipping a dir without errors: %+v \n", info.Name()) + return filepath.SkipDir + } + fmt.Printf("visited file: %q\n", path) + return nil + }) + + if err != nil { + fmt.Printf("error walking the path %q: %v\n", dir, err) + } +}