gpt4 book ai didi

go - 将参数传递给 GoLang 中的 filepath.Glob 函数

转载 作者:IT王子 更新时间:2023-10-29 01:03:28 31 4
gpt4 key购买 nike

几天来我一直在尝试使用 GoLang,我正在尝试制作与特定目录中的特定文件匹配的简单程序。但是我不知道如何将变量传递给 filepath.Glob 函数。

我的尝试:

func ReadDirectory(srcDir string) {
files, _ := filepath.Glob("[a-Z0-9]")
fmt.Println(files)
}

这一个很好地打印了我运行程序的当前目录。但是我正在寻找一种方法来列出传递给它的 srcDir 变量,这样我就可以从任何目录中找到文件。

最佳答案

只需在模式前加上目录:

files, _ := filepath.Glob(srcDir + "/[a-Z0-9]")

docs举个例子:

The pattern may describe hierarchical names such as /usr/*/bin/ed (assuming the Separator is /).

关于go - 将参数传递给 GoLang 中的 filepath.Glob 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20788486/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com