我遵循GoLang教程here,但無論出於什麼原因,我都無法讓應用程序服務於css和js。如果我運行我的靜態頁面沒有去服務器頁面的CSS工作正常。另一方面,當我運行go服務器時,css不起作用。如何在Go Lang中使用CSS和JS
這裏是我的HTML看起來有點像:
<link rel="stylesheet" href="../assets/css/bootstrap.min.css">
<link rel="stylesheet" href="../assets/css/bootstrap-theme.min.css">
<link rel="stylesheet" href="../assets/css/custom.css">
.
.
.
then under the body tag
<script src="../assets/js/jquery.min.js"></script>
<script src="../assets/js/bootstrap.min.js"></script>
.
.
.
我的文件樹是這個樣子
如何讓我的旅途中的應用程序服務的CSS和JavaScript我需要?
的問題已經被解決了,這裏是工作的主:
func main() {
http.HandleFunc("/view/", makeHandler(viewHandler))
http.HandleFunc("/edit/", makeHandler(editHandler))
http.HandleFunc("/save/", makeHandler(saveHandler))
http.HandleFunc("/index/", makeHandler(indexHandler))
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
http.ListenAndServe(":8080", nil)
}
這裏是我使用
func indexHandler(w http.ResponseWriter, r *http.Request, title string) {
p := &Page{Title: title}
err := templates.ExecuteTemplate(w, "index.html", p)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
[在Go模板中包含js文件]的可能重複(http://stackoverflow.com/questions/28899675/include-js-file-in-go-template/28899786#28899786);和[使用golang webserver,網站根目錄映射到文件系統?](http://stackoverflow.com/questions/28745161/with-golang-webserver-where-does-the-root-of-the-website -map-onto-the-filesystem/28745280) – icza