Read Directory Recursively

Use the filepath.Walk function to recursively walk down a directory.

The Walk function accepts a path, and WalkFunc as its parameters.

The WalkFunc signature is func(path string, info os.FileInfo, err error) error

In the WalkFunc if you want to skip an entire directory, return filepath.SkipDir which is a special error variable defined in the library.

package main

import (

func main() {
	path := "./dir"
	filepath.Walk(path, Walker)

func Walker(fn string, fi os.FileInfo, err error) error {
	if err != nil {
		fmt.Println("Walker Error: ", err)
		return err

	if fi.IsDir() {
		fmt.Println("Directory: ", fn)
		if fi.Name() == "skipme" {
			return filepath.SkipDir
	} else {
		fmt.Println("File: ", fn)
	return nil