我最近開(kāi)始學(xué)習(xí)圍棋,我想開(kāi)發(fā)一個(gè)簡(jiǎn)單的網(wǎng)站。但是我不知道如何為這個(gè)網(wǎng)站使用外部 CSS 文件。這是我的目錄結(jié)構(gòu):./ main.go static/ css/ home.css templates/ home.html這是我的 main.go 文件:package mainimport ( "html/template" "log" "net/http")func main() { http.HandleFunc("/", homeHandler) fs := http.FileServer(http.Dir("static/")) http.Handle("/static/", http.StripPrefix("/static/", fs)) log.Println("Listening on :8080...") log.Fatal(http.ListenAndServe(":8080", nil))}func homeHandler(w http.ResponseWriter, r *http.Request) { renderTemplate(w, "home")}func renderTemplate(w http.ResponseWriter, tmpl string) { t, err := template.ParseFiles("templates/" + tmpl + ".html") if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } err = t.Execute(w, nil) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) }}在 home.html 中,我在標(biāo)題中添加了這個(gè):<link rel="stylesheet" href="/css/home.css">在瀏覽器中調(diào)試時(shí),似乎找到了該文件,但 MIME 類(lèi)型出現(xiàn)錯(cuò)誤:瀏覽器控制臺(tái)屏幕截圖我認(rèn)為這兩行可以解決這個(gè)問(wèn)題,但顯然它沒(méi)有:fs := http.FileServer(http.Dir("static/"))http.Handle("/static/", http.StripPrefix("/static/", fs))有人知道我做錯(cuò)了什么嗎?
無(wú)法在 Go webapp 中提供外部 CSS 文件
翻翻過(guò)去那場(chǎng)雪
2022-06-06 17:47:40