登录
首页 >  Golang >  Go问答

查找所有扩展名的文件的golang文件名正则表达式是什么?

来源:stackoverflow

时间:2024-03-15 14:27:29 461浏览 收藏

从现在开始,努力学习吧!本文《查找所有扩展名的文件的golang文件名正则表达式是什么?》主要讲解了等等相关知识点,我会在golang学习网中持续更新相关的系列文章,欢迎大家关注并积极留言建议。下面就先一起来看一下本篇正文内容吧,希望能帮到你!

问题内容

下面的代码可以打开名为 rx80_aws.png 的文件,但我想使用 rx80_aws* 打开文件,无论扩展名如何,因为文件名是唯一的,但我们在您的文件夹中上传 .png .pdf 和 .jpeg 文件

func DownloadCert(w http.ResponseWriter, r *http.Request) {
    Openfile, err := os.Open("./certificate/rx80_AWS.png") //Open the file to be downloaded later
    defer Openfile.Close()                                 //Close after function return
    fmt.Println("FIle:", files)
    if err != nil {
        http.Error(w, "File not found.", 404) //return 404 if file is not found
        return
    }

    tempBuffer := make([]byte, 512)                       //Create a byte array to read the file later
    Openfile.Read(tempBuffer)                             //Read the file into  byte
    FileContentType := http.DetectContentType(tempBuffer) //Get file header

    FileStat, _ := Openfile.Stat()                     //Get info from file
    FileSize := strconv.FormatInt(FileStat.Size(), 10) //Get file size as a string

    Filename := attuid + "_" + skill

    //Set the headers
    w.Header().Set("Content-Type", FileContentType+";"+Filename)
    w.Header().Set("Content-Length", FileSize)

    Openfile.Seek(0, 0)  //We read 512 bytes from the file already so we reset the offset back to 0
    io.Copy(w, Openfile) //'Copy' the file to the client

}

正确答案


使用filepath.Glob

files, err := filepath.Glob("certificate/rx80_AWS*")
if err != nil {
   // handle errors
}
for _, filename in files {
   //...handle each file...
}

Here is an example that works with the playground 通过匹配 /bin/*cat (匹配 catzcat 等)。

理论要掌握,实操不能落!以上关于《查找所有扩展名的文件的golang文件名正则表达式是什么?》的详细介绍,大家都掌握了吧!如果想要继续提升自己的能力,那么就来关注golang学习网公众号吧!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>