【问题标题】:How do i open all files of the format "test*.txt" in golang如何在 golang 中打开所有格式为“test*.txt”的文件
【发布时间】:2020-04-30 10:24:55
【问题描述】:

如果我将标志(在 golang 中)设置为“test*.txt”(作为 CLI 参数),我需要打开上述格式的所有文件(例如:test.txt、test1.txt、test2.txt )。

我正在考虑对文件夹中存在的所有文件进行正则表达式匹配。有没有更好的办法?

【问题讨论】:

    标签: regex go unix grep go-flag


    【解决方案1】:

    你可以使用filepath.Glob函数:

    package main
    
    import (
        "fmt"
        "path/filepath"
    )
    
    func main() {
        matches, _ := filepath.Glob("test*.txt")
        for _, p := range matches {
            fmt.Println(p)
        }
    }
    
    

    【讨论】:

      猜你喜欢
      • 2021-03-11
      • 2017-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-08
      • 2021-06-30
      • 1970-01-01
      相关资源
      最近更新 更多