How to use a double star globe in Go?

It seems to be Goone of the few languages ​​that does not seem to understand the double star syntax ("globstar") for file globbing. At least this doesn't work as expected:

filepath.Glob(dir + "/**/*.bundle/*.txt")

Am I missing something in the implementation filepath? Is there a library around that supports this?

+4
source share
2 answers

filepath.Glob filepath.Match . (.gitignore, zsh) . - :

func glob(dir string, ext string) ([]string, error) {

  files := []string{}
  err := filepath.Walk(dir, func(path string, f os.FileInfo, err error) error {
    if filepath.Ext(path) == ext {
      files = append(files, path)
    }
    return nil
  })

  return files, err
}

.

+1

, . :

$ find a
a
a/b
a/b/c.d
a/b/c.d/e.f

a/**/*.* a, , :

package main

import (
  "fmt"

  "github.com/yargevad/filepathx"
)

func main() {
  matches, err := filepathx.Glob("./a/**/*.*")
  if err != nil {
    panic(err)
  }

  for _, match := range matches {
    fmt.Printf("MATCH: [%v]\n", match)
  }
}

:

$ go run example.go
MATCH: [a/b/c.d]
MATCH: [a/b/c.d/e.f]
+1

All Articles