0
index.go转到模板包括
package main
import (
"html/template"
"net/http"
)
func viewHandler(w http.ResponseWriter, r *http.Request) {
t, _ := template.ParseFiles("index.html")
t.Execute(w, nil)
}
func main() {
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
http.HandleFunc("/index", viewHandler)
http.ListenAndServe(":8080", nil)
}
在我的index.html,我用下面的路径
<link rel="stylesheet" type="text/css" href="/static/css/bootstrap.css">
而为的.css如下路径,
外部CSSweb(文件夹)
| --- index.go
| --- static/css/xxx.css
但是,CSS不包含在html中。如何更改代码来修复此问题
你是什么意思“未包含在html中”?根据代码,服务器应该在serveraddress.com/static/css/bootstrap.css下提供CSS文件,而不是将其包含在任何内容中。我很确定你写在这里的文件名是错误的。 “index.go”? – Staven
@Staven对不起,这是main.go.其实,当我点击http:// localhost:8080/static/css/bootstrap.css时,它会告诉404错误。我该如何解决它? – Wyatt
@Wyatt我刚刚测试了你的代码,它在我的机器上正确地提供了CSS文件。你确定你的机器上没有与8080端口有冲突吗? – Intermernet