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

Examples

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

Reference

  1. Go Spec