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. Print String
  8. String Backslash Escape
  9. Rune
  10. Variable
  11. Zero Value
  12. Constant
  13. If Then Else
  14. Switch/Case
  15. Loop
  16. Basic Types
  17. Array
  18. Slice
  19. Map
  20. Struct
  21. Function
  22. Closure
  23. Pointer
  24. String, Byte Slice, Rune Slice
  25. regexp
  26. Read File
  27. Write to File
  28. Walk Dir
  29. Check File Exist
  30. System Call
  31. Get Script Path
  32. Defer
  33. Random Number

Examples

  1. Match Any Regexp
  2. Find Replace
  3. Validate Links
  4. Generate Sitemap

Reference

  1. Go Spec