1 回答

TA貢獻1816條經驗 獲得超6個贊
net/http.Handle你不能混合gorilla/mux.Router
你可以這樣做
func main() {
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("./static/"))))
http.HandleFunc("/index", index)
http.ListenAndServe(":8080", nil)
}
func index(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "./static/html/test.html")
}
或者像這樣
func main() {
r := mux.NewRouter()
r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir("./static/"))))
r.HandleFunc("/index", index)
http.ListenAndServe(":8080", r)
}
func index(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "./static/html/test.html")
}
- 1 回答
- 0 關注
- 120 瀏覽
添加回答
舉報