1 回答

TA貢獻1842條經驗 獲得超13個贊
有一個 hacky 解決方案,利用這樣一個事實,即您可以在完成其工作http.ResponseWriter后繼續寫入。http.FileServer一般不推薦,但在這種情況下它可能是可以接受的。
package main
import (
"io"
"log"
"net/http"
)
const (
link = `<link rel="stylesheet" href="/path/to/style.css">`
)
func main() {
fs := http.FileServer(http.Dir("/tmp"))
var handler http.HandlerFunc
handler = func(w http.ResponseWriter, r *http.Request) {
var (
url = r.URL.Path
isDir = url[len(url)-1] == '/'
)
fs.ServeHTTP(w, r)
if isDir {
io.WriteString(w, link)
}
}
log.Fatal(http.ListenAndServe(":8080", handler))
}
- 1 回答
- 0 關注
- 114 瀏覽
添加回答
舉報