Golang: Walk Directory

By Xah Lee. Date: . Last updated: .

to go thru all files and directory, use

filepath.Walk(dir_path, process_func)

from package path/filepath

package main

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

var myDir = "/Users/x/web/"

// go thru a dir and print all file name and extension

func main() {
    // the function that handles each file or dir
    var ff = func(pathX string, infoX os.FileInfo, errX error) error {

    // first thing to do, check error. and decide what to do about it
        if errX != nil {
        fmt.Printf("error 「%v」 at a path 「%q」\n", errX, pathX)
        return errX
    }

    fmt.Printf("pathX: %v\n", pathX)

    // find out if it's a dir or file, if file, print info
        if infoX.IsDir() {
        fmt.Printf("is dir.\n")
    } else {
        fmt.Printf("  dir: 「%v」\n", filepath.Dir(pathX))
        fmt.Printf("  file name 「%v」\n", infoX.Name())
        fmt.Printf("  extenion: 「%v」\n", filepath.Ext(pathX))
    }

    return nil
    }

    err := filepath.Walk(myDir, ff)

    if err != nil {
    fmt.Printf("error walking the path %q: %v\n", myDir, err)
    }
}

filepath.Walk(dir_path, ff) walks dir_path, for each file or dir, it calls ff

when there's a problem, a error is passed to ff. ff can do whatever with it.

ff must return one of:

Argument Passed to the Walker Function

filepath.Walk(myDir, ff) passes 3 arguments to ff.

ff is the walker function that you must write. It process each path.

The arguments passed to the walker are:

In your walker function, you must delare the types accordingly. e.g.

var walker = func(pathX string, infoX os.FileInfo, errX error) error {…}

FileInfo

A FileInfo describes a file and is returned by Stat and Lstat.

type FileInfo interface {
    Name() string       // base name of the file
    Size() int64        // length in bytes for regular files; system-dependent for others
    Mode() FileMode     // file mode bits
    ModTime() time.Time // modification time
    IsDir() bool        // abbreviation for Mode().IsDir()
    Sys() interface{}   // underlying data source (can return nil)
}

error

The error built-in interface type is the conventional interface for representing an error condition, with the nil value representing no error.

type error interface {
    Error() string
}

Alternative

there is a well known problem that filepath.Walk is slow, not implemented optimally.

see https://github.com/golang/go/issues/16399

here's alternative: https://github.com/karrick/godirwalk/blob/master/README.md

If you have a question, put $5 at patreon and message me.

Golang

  1. Compile, Run
  2. Source Encoding
  3. Package, Import
  4. Comment
  5. Print
  6. String
  7. Rune
  8. String as Chars
  9. Variable
  10. Zero Value
  11. Constant
  12. If Then Else
  13. Switch/Case
  14. Loop
  15. Basic Types
  16. Array
  17. Slice
  18. Map
  19. Struct
  20. Function
  21. regexp
  22. Read File
  23. Write to File
  24. Walk Dir
  25. Check File Exist
  26. System Call
  27. Get Script Path
  28. Pointer
  29. Defer
  30. Random Number

Examples

  1. match any regexp
  2. Validate Links
  3. Generate Sitemap

Reference

  1. Go Spec