Pesquisa Recursiva com Golang

Exemplo de como fazer uma pesquisa de pastas com o nome node_modules

Exemplo de Código Fonte em Golang para pesquisa recursiva em diretórios

package main

import (
    "fmt"
    "os"
    "path"
    "path/filepath"
    "regexp"
)

func run() ([]string, error) {
    searchDir := "l:/projetos"

    fileList := make([]string, 0)
    e := filepath.Walk(searchDir, func(pathx string, f os.FileInfo, err error)
     error {
        if f.IsDir() {
            if path.Base(f.Name()) == "node_modules" {
                nm := regexp.MustCompile("node_modules")
                matches := nm.FindAllStringIndex(pathx, -1)
                if (len(matches)) == 1 {
                    fileList = append(fileList, pathx)
                }
            }
        }
        return err
    })

    if e != nil {
        panic(e)
    }

    for _, file := range fileList {
        fmt.Println(file)
    }

    return fileList, nil
}

func main() {
    run()
}

Last updated

Was this helpful?