Skip to content

Commit 97590ae

Browse files
jfcantinianlancetaylor
authored andcommitted
path/filepath: add example for Walk
Fixes: #22052 Change-Id: Ia056871b35ecc1a8c5ac891402fc1c5702731623 Reviewed-on: https://go-review.googlesource.com/66330 Run-TryBot: Ian Lance Taylor <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent 71d0832 commit 97590ae

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

src/path/filepath/example_unix_test.go

+22
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package filepath_test
88

99
import (
1010
"fmt"
11+
"os"
1112
"path/filepath"
1213
)
1314

@@ -79,3 +80,24 @@ func ExampleJoin() {
7980
// a/b/c
8081
// a/b/c
8182
}
83+
func ExampleWalk() {
84+
dir := "dir/to/walk"
85+
subDirToSkip := "skip" // dir/to/walk/skip
86+
87+
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
88+
if err != nil {
89+
fmt.Printf("prevent panic by handling failure accessing a path %q: %v\n", dir, err)
90+
return err
91+
}
92+
if info.IsDir() && info.Name() == subDirToSkip {
93+
fmt.Printf("skipping a dir without errors: %+v \n", info.Name())
94+
return filepath.SkipDir
95+
}
96+
fmt.Printf("visited file: %q\n", path)
97+
return nil
98+
})
99+
100+
if err != nil {
101+
fmt.Printf("error walking the path %q: %v\n", dir, err)
102+
}
103+
}

0 commit comments

Comments
 (0)