1
0
mirror of https://github.com/golang/go synced 2024-11-17 20:54:48 -07:00

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>
This commit is contained in:
Jean-Francois Cantin 2017-09-26 16:07:27 -06:00 committed by Ian Lance Taylor
parent 71d08324ed
commit 97590aea67

View File

@ -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)
}
}