path/filepath

  • func Base(path string) string
  • func Dir(path string) string
  • func Ext(path string) string
  • func HasPrefix(p, prefix string) bool
  • func Join(elem ...string) string
  • func Walk(root string, walkFn WalkFunc) error

func Walk(root string, walkFn WalkFunc) error

Walk walks the file tree rooted at root, calling walkFn for each file or directory in the tree, including root. All errors that arise visiting files and directories are filtered by walkFn. The files are walked in lexical order, which makes the output deterministic but means that for very large directories Walk can be inefficient. Walk does not follow symbolic links.

package main

import (
    "fmt"
    "io/ioutil"
    "os"
    "path/filepath"
)

func prepareTestDirTree(tree string) (string, error) {
    tmpDir, err := ioutil.TempDir("", "")
    if err != nil {
        return "", fmt.Errorf("error creating temp directory: %v\n", err)
    }

    err = os.MkdirAll(filepath.Join(tmpDir, tree), 0755)
    if err != nil {
        os.RemoveAll(tmpDir)
        return "", err
    }

    return tmpDir, nil
}

func main() {
    tmpDir, err := prepareTestDirTree("dir/to/walk/skip")
    if err != nil {
        fmt.Printf("unable to create test dir tree: %v\n", err)
        return
    }
    defer os.RemoveAll(tmpDir)
    os.Chdir(tmpDir)

    subDirToSkip := "skip"

    fmt.Println("On Unix:")
    err = filepath.Walk(".", 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", path, 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 or dir: %q\n", path)
        return nil
    })
    if err != nil {
        fmt.Printf("error walking the path %q: %v\n", tmpDir, err)
        return
    }
}
On Unix:
visited file or dir: "."
visited file or dir: "dir"
visited file or dir: "dir/to"
visited file or dir: "dir/to/walk"
skipping a dir without errors: skip

type WalkFunc

WalkFunc is the type of the function called for each file or directory visited by Walk. The path argument contains the argument to Walk as a prefix; that is, if Walk is called with "dir", which is a directory containing the file "a", the walk function will be called with argument "dir/a". The info argument is the os.FileInfo for the named path.

If there was a problem walking to the file or directory named by path, the incoming error will describe the problem and the function can decide how to handle that error (and Walk will not descend into that directory). In the case of an error, the info argument will be nil. If an error is returned, processing stops. The sole exception is when the function returns the special value SkipDir. If the function returns SkipDir when invoked on a directory, Walk skips the directory's contents entirely. If the function returns SkipDir when invoked on a non-directory file, Walk skips the remaining files in the containing directory.

type WalkFunc func(path string, info os.FileInfo, err error) error

results matching ""

    No results matching ""