douye2020 2014-11-23 18:56
浏览 66
已采纳

如何通过文件名(无扩展名)检查文件是否存在?

Question is quite self-explanatory.

Please, could anybody show me how can I check existence of the file by name (without extension) by short and efficient way. It would be great if code returns several occurrence if folder have several files with the same name.

Example:

folder/
  file.html
  file.md

UPDATE:

It is not obviously how to use one of filepath.Match() or filepath.Glob() functions by official documentation. So here is some examples:

matches, _          := filepath.Glob("./folder/file*") //returns paths to real files [folder/file.html, folder/file.md]
matchesToPattern, _ := filepath.Match("./folder/file*", "./folder/file.html") //returns true, but it is just compare strings and doesn't check real content
  • 写回答

2条回答 默认 最新

  • dongzi1209 2014-11-23 19:32
    关注

    You need to use the path/filepath package.

    The functions to check are: Glob(), Match() and Walk() — pick whatever suits your taste better.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?