Last active
September 13, 2022 08:02
-
-
Save tomcam/db640cfc7b846e083f4f7dec19cf345d to your computer and use it in GitHub Desktop.
dl.go - Lists directories in Go. Simplest use of Golang's filepath.Walk() with easily reusable code. golang list directories recursive.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// dl displays a list of directories. Starts at current dir unless | |
// another is given on the command line. Counts theme. | |
// Naively written; doesn't do a good job when you don't have | |
// the right permissions. | |
// Usage: | |
// dl [directory] | |
package main | |
import ( | |
"fmt" | |
"os" | |
"path/filepath" | |
) | |
func visit(files *[]string, count *int) filepath.WalkFunc { | |
return func(path string, info os.FileInfo, err error) error { | |
if err != nil { | |
fmt.Printf("Error at %v: %v", path, err.Error()) | |
return err | |
} | |
isDir := info.IsDir() | |
if isDir { | |
if path != "." && path != ".." { | |
fmt.Printf("%s\n", path) | |
*count++ | |
*files = append(*files, path) | |
} | |
} | |
return nil | |
} | |
} | |
// Obtain a list of all files in the specified project tree starting | |
// at the root. | |
// Ignore directories starting with a . | |
// Ignore the assets directory | |
func getDirTree(path string, dirs *int) (tree []string, err error) { | |
var files []string | |
err = filepath.Walk(path, visit(&files, dirs)) | |
if err != nil { | |
fmt.Printf("Error at %v: %v", path, err.Error()) | |
return []string{}, err | |
} | |
return files, nil | |
} | |
func main() { | |
dir := "." | |
if len(os.Args) > 1 { | |
dir = os.Args[1] | |
} | |
dirCount := 0 | |
_, err := getDirTree(dir, &dirCount) | |
if err != nil { | |
fmt.Printf("Error: %v", err.Error()) | |
} else { | |
if dirCount != 1 { | |
fmt.Printf("%v directories\n", dirCount) | |
} else { | |
fmt.Printf("%v directory\n", dirCount) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment