您好我刚刚开始在golang中学习html / templates。我得到的错误是系统找不到指定的文件路径'。文件路径是templates / time.html。 time.html的位置(我试图渲染的页面)是
的src /模板/为time.html
我的主要位置是src / timeserver / timerserver.go
这里是我使用的代码
func TimeServer(w http.ResponseWriter, req *http.Request) {
// if user goes to another website after time/...
if req.URL.Path != "/time/" {
errorHandler(w, req, http.StatusNotFound)
return
}
cookie, _ := req.Cookie("UUID")
//existCheck := false
//temp2 := ""
profile := Profile{"",time.Now().Format("3:04:04 PM")}
if cookie != nil { // if cookie exist set flags
name, check := cookieJar.GetValue(cookie.Value)
profile = Profile{name,time.Now().Format("3:04:04 PM")}
fmt.Println(name)
//existCheck = check
//temp2 = name
fmt.Println(check)
}
fp := path.Join("templates", "time.html")
tmpl, err := template.ParseFiles(fp)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if err := tmpl.Execute(w, profile); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
答案 0 :(得分:0)
问题是我的道路不正确。改变了
fp := path.Join("templates", "time.html")
到
fp := path.Join("Home/go/src/templates", "time.html")