亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為什么我不能使用 gorilla/mux.Router net/http.Handle

為什么我不能使用 gorilla/mux.Router net/http.Handle

Go
白豬掌柜的 2022-12-19 20:30:39
我查看了所有類似的問題并按照那里所說的那樣連接了文件,但盡管如此,該文件還是不起作用。我不知道該怎么辦,我做錯了什么主程序func main() {    r := mux.NewRouter()    http.Handle("/static/", 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")}結構體測試.html<!DOCTYPE html><html>    <head>        <link rel="stylesheet" type="text/css" href="/static/css/test.css" />    </head>    <body class="sb-nav-fixed">        asdfasd    </body></html>測試.cssbody{    height: 100%;    width: 100%;    background-color: brown;}
查看完整描述

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")

}


查看完整回答
反對 回復 2022-12-19
  • 1 回答
  • 0 關注
  • 120 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號