diff options
| author | Jean-Francois Cantin <jfcantin@gmail.com> | 2017-09-26 16:07:27 -0600 |
|---|---|---|
| committer | Ian Lance Taylor <iant@golang.org> | 2017-09-27 14:52:58 +0000 |
| commit | 97590aea672d2fccffc28162eddda19ec04fa328 (patch) | |
| tree | 8e6f2a974ccf6ae26924f89ad43818744dd7f5e7 /src/path | |
| parent | 71d08324ed0f842228ee8aa966736a157b4a2422 (diff) | |
| download | go-97590aea672d2fccffc28162eddda19ec04fa328.tar.xz | |
path/filepath: add example for Walk
Fixes: #22052
Change-Id: Ia056871b35ecc1a8c5ac891402fc1c5702731623
Reviewed-on: https://go-review.googlesource.com/66330
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Diffstat (limited to 'src/path')
| -rw-r--r-- | src/path/filepath/example_unix_test.go | 22 |
1 files changed, 22 insertions, 0 deletions
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) + } +} |
