dql1978dql1978 2017-06-15 08:55
浏览 311
已采纳

http.Request:从网址获取文件名

How do I get only the file name one.json from the following request: http://localhost/slow/one.json?

I just need to serve this file and others from the url? This is a test server that I need to respond very slow.

http.HandleFunc("/slow/", func(w http.ResponseWriter, r *http.Request) {
    log.Println("Slow...")
    log.Println(r.URL.Path[1:])
    time.Sleep(100 * time.Millisecond)
    http.ServeFile(w, r, r.URL.Path[1:])
})
  • 写回答

2条回答 默认 最新

  • duanfei1268 2017-06-15 14:38
    关注

    I believe you are looking for path.Base: "Base returns the last element of path."

    r,_ := http.NewRequest("GET", "http://localhost/slow/one.json", nil)
    fmt.Println(path.Base(r.URL.Path))
    // one.json
    

    Playground link

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

报告相同问题?

悬赏问题

  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥30 vmware exsi重置后登不上
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?