惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Latest news
Latest news
T
Troy Hunt's Blog
V
Vulnerabilities – Threatpost
L
LINUX DO - 热门话题
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Simon Willison's Weblog
Simon Willison's Weblog
V
V2EX
博客园 - 司徒正美
B
Blog RSS Feed
AWS News Blog
AWS News Blog
MyScale Blog
MyScale Blog
Scott Helme
Scott Helme
Cisco Talos Blog
Cisco Talos Blog
Last Week in AI
Last Week in AI
NISL@THU
NISL@THU
博客园 - Franky
P
Proofpoint News Feed
博客园_首页
C
CERT Recently Published Vulnerability Notes
雷峰网
雷峰网
S
Schneier on Security
P
Proofpoint News Feed
Hugging Face - Blog
Hugging Face - Blog
G
GRAHAM CLULEY
博客园 - 三生石上(FineUI控件)
月光博客
月光博客
WordPress大学
WordPress大学
The Hacker News
The Hacker News
T
Threatpost
阮一峰的网络日志
阮一峰的网络日志
A
Arctic Wolf
Microsoft Azure Blog
Microsoft Azure Blog
T
The Exploit Database - CXSecurity.com
Engineering at Meta
Engineering at Meta
罗磊的独立博客
T
The Blog of Author Tim Ferriss
D
Darknet – Hacking Tools, Hacker News & Cyber Security
I
Intezer
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
K
Kaspersky official blog
SecWiki News
SecWiki News
云风的 BLOG
云风的 BLOG
美团技术团队
C
Cybersecurity and Infrastructure Security Agency CISA
博客园 - 【当耐特】
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Security Latest
Security Latest
C
Cyber Attacks, Cyber Crime and Cyber Security
B
Blog
S
Security Affairs

博客园 - 步孤天

程序员去新加坡打工的杂事记录 基于Bert的中文评价情感分析 (转载)DeepSeek+LoRA+FastAPI-微调大模型并暴露接口给后端调用 用YOLOv5截取出短剧的人物 Chromium源码分析五:写一个利用ipc+protobuf通信的demo Chromium源码分析四:RunLoop、Bind、scoped_refptr Chromium源码分析三:Chromium中用到的设计模式 Chromium源码分析二:LifeofaPixel.pdf Chromium源码分析一:基础知识 交叉编译valgrind在嵌入式设备上调试程序 gerrit 反向代理从 apache 换成 nginx 之后项目页报错“The page you requested was not found, or you do not have permission to view this page” 六十花甲子纳音表中的五行是怎么算出来的 df查看30GB的磁盘满了而du -sh查看磁盘占用只有6GB centos7+mariadb安装在线评判系统 如何去掉Linux vim文本中的^M 如何从超大(10G)sql语句文本中分离出需要的部分 golang如何打印变量类型,golang list如何把元素转换为可用类型 数据库文件导入报错"MySQL server has gone away" 如何在Linux上用tshark命令把抓包中follow的二进制流保存成文件
golang实现一个简单的文件浏览下载功能代码示例
步孤天 · 2023-10-07 · via 博客园 - 步孤天

想省事用Claude(一个 依托chatgpt 的 AI)生成一段 golang 的文件浏览下载示例,结果给生成的代码大概是这样的(省去了无关部分,主要部分如下):

    http.HandleFunc("/*", downloadFile)
    http.HandleFunc("/", showFileList)

测试之后,结果发现每次都会走到“/”下去,无论如何都不会走到下载上。通配符“/*”竟然不生效,把两个函数换位置也无效。
拿这个结果再问 Claude,他给出了建议,但依然无法解决,最终还是自己取文件明判断,然后再跳转到处理函数才解决了此问题。

/*这个示例在`./files`目录下存放文件,主页`/`路径展示文件列表。点击文件链接会下载该文件。
可以根据需要调整文件存储路径,添加更多功能等。*/

package main

import (
    "fmt"
    "io"
    "net/http"
    "os"
    "path/filepath"
)

// 指定文件存储目录
var fileDir string

func main() {
    fileDir = "./files"
    http.HandleFunc("/", dispatcher)
    http.ListenAndServe(":8080", nil)
}

func dispatcher(w http.ResponseWriter, r *http.Request) {
    // 获取文件名
    file := filepath.Base(r.URL.Path)
    fmt.Println(file)
    if len(file) > 0 && file != "/" {
        downloadFile(w, r)
    }else {
        showFileList(w, r)
    }
}

func downloadFile(w http.ResponseWriter, r *http.Request) {
    //下载文件逻辑
    fmt.Println("run to /*")
    file := r.URL.Path[1:]
    f, _ := os.Open(fileDir + "/" + file)
    defer f.Close()
    
    w.Header().Set("Content-Type", "application/octet-stream")
    w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", file))
    
    io.Copy(w, f)
}

func showFileList(w http.ResponseWriter, r *http.Request) {
    //展示文件列表逻辑  
    fmt.Println("run to /")
    files, _ := filepath.Glob(fileDir + "/*")
    
    fmt.Fprint(w, "<h1>文件列表:</h1><ul>")
    for _, f := range files {
        fmt.Fprintf(w, `<li><a href="/%s">%s</a></li>`, filepath.Base(f), filepath.Base(f)) 
    }
    fmt.Fprint(w, "</ul>")
}