Commit 97590aea authored by Jean-Francois Cantin's avatar Jean-Francois Cantin Committed by Ian Lance Taylor

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: 's avatarIan Lance Taylor <iant@golang.org>
parent 71d08324
......@@ -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)
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment