mirror of
https://github.com/golang/go.git
synced 2025-10-19 11:03:18 +00:00
io/fs: add examples for Glob,ReadFile and ValidPath
Change-Id: I8451179bc0fa88b7e60afbc6fd9e06a22a94f3aa Reviewed-on: https://go-review.googlesource.com/c/go/+/673835 Reviewed-by: Sean Liao <sean@liao.dev> Auto-Submit: Sean Liao <sean@liao.dev> Reviewed-by: Michael Knyszek <mknyszek@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: David Chase <drchase@google.com>
This commit is contained in:
parent
3fd729b2a1
commit
787362327f
1 changed files with 81 additions and 0 deletions
|
@ -9,8 +9,89 @@ import (
|
||||||
"io/fs"
|
"io/fs"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
"testing/fstest"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func ExampleGlob() {
|
||||||
|
fsys := fstest.MapFS{
|
||||||
|
"file.txt": {},
|
||||||
|
"file.go": {},
|
||||||
|
"dir/file.txt": {},
|
||||||
|
"dir/file.go": {},
|
||||||
|
"dir/subdir/x.go": {},
|
||||||
|
}
|
||||||
|
|
||||||
|
patterns := []string{
|
||||||
|
"*.txt",
|
||||||
|
"*.go",
|
||||||
|
"dir/*.go",
|
||||||
|
"dir/*/x.go",
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, pattern := range patterns {
|
||||||
|
matches, err := fs.Glob(fsys, pattern)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
fmt.Printf("%q matches: %v\n", pattern, matches)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// "*.txt" matches: [file.txt]
|
||||||
|
// "*.go" matches: [file.go]
|
||||||
|
// "dir/*.go" matches: [dir/file.go]
|
||||||
|
// "dir/*/x.go" matches: [dir/subdir/x.go]
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleReadFile() {
|
||||||
|
fsys := fstest.MapFS{
|
||||||
|
"hello.txt": {
|
||||||
|
Data: []byte("Hello, World!\n"),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
data, err := fs.ReadFile(fsys, "hello.txt")
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Print(string(data))
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// Hello, World!
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleValidPath() {
|
||||||
|
paths := []string{
|
||||||
|
".",
|
||||||
|
"x",
|
||||||
|
"x/y/z",
|
||||||
|
"",
|
||||||
|
"..",
|
||||||
|
"/x",
|
||||||
|
"x/",
|
||||||
|
"x//y",
|
||||||
|
"x/./y",
|
||||||
|
"x/../y",
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, path := range paths {
|
||||||
|
fmt.Printf("ValidPath(%q) = %t\n", path, fs.ValidPath(path))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// ValidPath(".") = true
|
||||||
|
// ValidPath("x") = true
|
||||||
|
// ValidPath("x/y/z") = true
|
||||||
|
// ValidPath("") = false
|
||||||
|
// ValidPath("..") = false
|
||||||
|
// ValidPath("/x") = false
|
||||||
|
// ValidPath("x/") = false
|
||||||
|
// ValidPath("x//y") = false
|
||||||
|
// ValidPath("x/./y") = false
|
||||||
|
// ValidPath("x/../y") = false
|
||||||
|
}
|
||||||
|
|
||||||
func ExampleWalkDir() {
|
func ExampleWalkDir() {
|
||||||
root := "/usr/local/go/bin"
|
root := "/usr/local/go/bin"
|
||||||
fileSystem := os.DirFS(root)
|
fileSystem := os.DirFS(root)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue