Skip to content

Commit 831573c

Browse files
adammithaodeke-em
authored andcommitted
io/fs: added an example for io/fs.WalkDir
The documentation currently does not show how to get an `FS` instance for the operating system's filesystem. This example demonstrates how to accomplish this using the `os` package. Fixes #46083 Change-Id: I053111c12ab09ef13f0d04fcdff8a6ea0dccf379 Reviewed-on: https://go-review.googlesource.com/c/go/+/319989 Run-TryBot: Ian Lance Taylor <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Trust: Emmanuel Odeke <[email protected]>
1 parent baa934d commit 831573c

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

src/io/fs/example_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright 2021 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package fs_test
6+
7+
import (
8+
"fmt"
9+
"io/fs"
10+
"log"
11+
"os"
12+
)
13+
14+
func ExampleWalkDir() {
15+
root := "/usr/local/go/bin"
16+
fileSystem := os.DirFS(root)
17+
18+
fs.WalkDir(fileSystem, ".", func(path string, d fs.DirEntry, err error) error {
19+
if err != nil {
20+
log.Fatal(err)
21+
}
22+
fmt.Println(path)
23+
return nil
24+
})
25+
}

0 commit comments

Comments
 (0)