dongshuql24533
2013-12-26 17:12
浏览 618
已采纳

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

I've been trying to get my head around with GoLang for few days and I am trying to make simple program which matches to certain files in certain directory. However I don't know how to pass variable to filepath.Glob function.

My attempt:

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

This one prints well current directory where I am running the program. However I am looking for a way to list pass srcDir variable to it so I can find files from whatever directory.

图片转代码服务由CSDN问答提供 功能建议

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

我的尝试:

  func ReadDirectory(srcDir字符串)  {
文件,_:= filepath.Glob(“ [a-Z0-9]”)
 fmt.Println(文件)
} 
   
 
 

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

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • donljt2606 2013-12-26 17:21
    已采纳

    Just prefix the pattern with the directory:

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

    The docs give this example:

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

    打赏 评论

相关推荐 更多相似问题