2015-06-28 84 views
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如下路徑,

外部CSS

web(文件夾)

| --- index.go

| --- static/css/xxx.css

但是,CSS不包含在html中。如何更改代碼來修復此問題

+0

你是什麼意思「未包含在html中」?根據代碼,服務器應該在serveraddress.com/static/css/bootstrap.css下提供CSS文件,而不是將其包含在任何內容中。我很確定你寫在這裏的文件名是錯誤的。 「index.go」? – Staven

+0

@Staven對不起,這是main.go.其實,當我點擊http:// localhost:8080/static/css/bootstrap.css時,它會告訴404錯誤。我該如何解決它? – Wyatt

+0

@Wyatt我剛剛測試了你的代碼,它在我的機器上正確地提供了CSS文件。你確定你的機器上沒有與8080端口有衝突嗎? – Intermernet

回答

0

由於端口的衝突,CSS文件未正確包含。感謝@Intermernet。 :)