Html 在不同目录级别上提供css的Golang模板

Html 在不同目录级别上提供css的Golang模板,html,css,go,Html,Css,Go,我正在使用golang的html/模板包,以相同的_base.html作为框架,在多个页面上提供内容。我将多个html文件_base.html和内容文件合并为一个 func main() { http.HandleFunc("/", indexHandler) http.HandleFunc("/blog/", blogHandler) http.Handle("/css/", http.StripPrefix("/css/", http.FileServer(http.Dir("http/cs

我正在使用golang的html/模板包,以相同的_base.html作为框架,在多个页面上提供内容。我将多个html文件_base.html和内容文件合并为一个

func main() {
http.HandleFunc("/", indexHandler)
http.HandleFunc("/blog/", blogHandler)
http.Handle("/css/", http.StripPrefix("/css/", http.FileServer(http.Dir("http/css"))))
http.ListenAndServe(":1337", nil)
}

func indexHandler(w http.ResponseWriter, r *http.Request) {
index := template.Must(template.ParseFiles(
    "http/html/_base.html",
    "http/html/index.html",
))
index.Execute(w, nil)
}

func blogHandler(w http.ResponseWriter, r *http.Request) {
blog := template.Must(template.ParseFiles(
    "http/html/_base.html",
    "http/html/blog.html",
))
blog.Execute(w, nil)
}
在我的Web服务器的根目录上执行此操作,我的css呈现得很好,因为_base.html中my.css的html链接标记指向正确的目录,使用:

<link href="css/style.css" rel="stylesheet">
但是,当我从/blog/导航到/blog/时,我的css下降了一级或上升了一级,但是您希望看到它,因此css href突然指向/blog/css/style.css,因此它不会呈现


这可以很容易地修复,说明我与_base.html合并的每个内容文件中css的级别,但是我觉得必须有另一种更干净、不同的方式。在这种情况下,是不是我的直觉判断有误?

没有测试它,所以我不太确定,但是改变一下怎么样

<link href="css/style.css" rel="stylesheet">

?

<link href="/css/style.css" rel="stylesheet">