From 16cb002a8dab90d7e01ee13ab75d8a4661d77a84 Mon Sep 17 00:00:00 2001 From: gutmet Date: Sun, 23 Dec 2018 15:51:20 +0100 Subject: [PATCH] add 'DirsWithPrefix' function --- goutil.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/goutil.go b/goutil.go index 3db56d1..cc2f515 100644 --- a/goutil.go +++ b/goutil.go @@ -85,6 +85,22 @@ func PathExists(path string) bool { } } +func DirsWithPrefix(path string, prefix string) ([]string, error) { + dirs := []string{} + files, err := ioutil.ReadDir(path) + if err != nil { + return dirs, err + } + for _, file := range files { + if file.IsDir() { + if name := file.Name(); strings.HasPrefix(name, prefix) { + dirs = append(dirs, name) + } + } + } + return dirs, nil +} + func AskFor(question string) (string, error) { fmt.Print(question + ": ") reader := bufio.NewReader(os.Stdin)