#Skip to menu

Golang: File Tree Traversal (filepath.Walk)

In this article we’ll see how to walk the file system with golang. We’ll see:


As a simple example of filepath.Walk we’ll list all the files under a directory recursively (simple.go):

We set the log flags to Lshortfile to better spot errors when they happen. Everything else is explained very well in the go docs.

Run it with:

$ go run simple.go .

Passing arguments

We can pass arguments to filepath.WalkFunc trought a closure. printFile now doesn’t process the files directly but returns a closure that does the work. The closure can access the arguments we pass to printFile as if they were local variables. For example to ignore some directories we can pass a list of said directories to printFile and whenever the closure finds a directory whose name is inside the list it will skip it by returning os.SkipDir (ignore.go):

Find file duplicates

For a more realistic application we’ll write a program that will find all the file duplicates under a directory. For each file we’ll store its crypto/sha512 digest inside a map. If the digest was already present, the file is a duplicate, otherwise we store its path using the digest as a key (fdup.go):

Let’s try it

In a terminal run:

$ mkdir test
$ cd test
$ echo 'run free, run GNU' > gnu
$ echo 'from outer space' > plan9
$ cp gnu free
$ cp plan9 outer
$ ls
free  gnu  outer  plan9
$ cd ..
$ go run fdup.go test
"test/gnu" is a duplicate of "test/free"
"test/plan9" is a duplicate of "test/outer"


Despite filepath.Walk’s usefulness it can not model all type of programs, one such program is du. Starting with one directory, du reports the cumulative size of the given directory and all its subdirectories recursively. The entries of a directory are read with os.Readdir (du.go):

Get the source code

Great Golang ebook

Hey! I'm writing an ebook on how to write great Golang code. If it sounds interesting write to greatgo@xojoc.pw to be updated.

See all the discussions around the web about this page.