我如何在golang中打开所有格式为“ test * .txt”的文件

问题描述 投票:0回答:1

[如果我给标记(在golang中)为'test * .txt'(作为CLI参数),则我需要打开所述格式的所有文件(例如:test.txt,test1.txt,test2.txt) 。

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

regex go unix grep go-flag
1个回答
1
投票

您可以使用filepath.Glob功能:

package main

import (
    "fmt"
    "path/filepath"
)

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

© www.soinside.com 2019 - 2024. All rights reserved.