Skip to content

Instantly share code, notes, and snippets.

@schaternik
Created October 18, 2019 12:49
Show Gist options
  • Save schaternik/06fdf53e7f580d4946514e1ee2751497 to your computer and use it in GitHub Desktop.
Save schaternik/06fdf53e7f580d4946514e1ee2751497 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"io"
"os"
"path"
)
func dirTree(out io.Writer, root string, printFiles bool) error {
dirRead, err := os.Open(root)
if err != nil {
return err
}
dirFiles, err := dirRead.Readdir(0)
if err != nil {
return err
}
for _, file := range dirFiles {
filename := file.Name()
fullPath := path.Join(root, filename)
if file.IsDir() {
fmt.Println(fullPath)
dirTree(out, fullPath, printFiles)
} else {
if printFiles == true {
fmt.Println(fullPath)
}
}
}
return nil
}
func main() {
out := os.Stdout
if !(len(os.Args) == 2 || len(os.Args) == 3) {
panic("usage go run main.go . [-f]")
}
path := os.Args[1]
printFiles := len(os.Args) == 3 && os.Args[2] == "-f"
err := dirTree(out, path, printFiles)
if err != nil {
panic(err.Error())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment