如何在Go中使用双星球?

时间:2014-11-07 20:22:51

标签: go glob

似乎Go是少数语言之一,似乎不理解文件通配符的双星(" globstar")语法。至少这似乎没有按预期工作:

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

我是否遗漏了filepath实施的内容? 是否有一个图书馆支持这个?

3 个答案:

答案 0 :(得分:5)

filepath.Glob实施使用了filepath.Match。结果the specs for that没有涵盖相当常见的(.gitignorezsh)双星模式。绝不一样 - 但对于我的用例,我设法用这个小功能解决它:

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 :(得分:1)

我写了一个小extension library支持双星球。使用此目录结构:

$ 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]

答案 2 :(得分:0)

这里有类似的东西。您可以通过回调来过滤文件:

def Ichimoku_Cloud(df):
        '''
        Get the values of Lines for Ichimoku Cloud
        args:
            df: Dataframe
        '''
        d = df.sort_index(ascending=False) # my Live NSE India data is in Recent -> Oldest order

        # Tenkan-sen (Conversion Line): (9-period high + 9-period low)/2))
        period9_high = d['HIGH'].rolling(window=9).max()
        period9_low = d['LOW'].rolling(window=9).min()
        tenkan_sen = (period9_high + period9_low) / 2


        # Kijun-sen (Base Line): (26-period high + 26-period low)/2))
        period26_high = d['HIGH'].rolling(window=26).max()
        period26_low = d['LOW'].rolling(window=26).min()
        kijun_sen = (period26_high + period26_low) / 2

        # Senkou Span A (Leading Span A): (Conversion Line + Base Line)/2))
        senkou_span_a = ((tenkan_sen + kijun_sen) / 2).shift(26)

        # Senkou Span B (Leading Span B): (52-period high + 52-period low)/2))
        period52_high = d['HIGH'].rolling(window=52).max()
        period52_low = d['LOW'].rolling(window=52).min()
        senkou_span_b = ((period52_high + period52_low) / 2).shift(26)

        # The most current closing price plotted 22 time periods behind (optional)
        chikou_span = d['CLOSE'].shift(-22) # Given at Trading View.

        d['blue_line'] = tenkan_sen
        d['red_line'] = kijun_sen
        d['cloud_green_line_a'] = senkou_span_a
        d['cloud_red_line_b'] = senkou_span_b
        d['lagging_line'] = chikou_span
        return d.sort_index(ascending=True)