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:])
})
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With