#26 - Directory walk in Go

Data: 2018-09-15 12:00 - Go

Walks directories in Go and returns files and directories separately.

func walkFiles(dir string, fnFile func(path string, info os.FileInfo), fnDir func(path string)) {
	files, _ := ioutil.ReadDir(dir)
	path := dir + "/"
	if path == ".//" {
		path = ""
	}
	for _, file := range files {
		if file.IsDir() {
            walkFiles(path+file.Name(), fnFile, fnDir)
            fnDir(path + file.Name())
		} else {
			fnFile(path+file.Name(), file)
		}
	}
}

Previous snippet | Next snippet