簡體   English   中英

使用GoLang模板:系統找不到指定的路徑

[英]Using GoLang templates: System cannot find path specified

您好,我剛剛開始學習golang中的html / templates。 我得到的錯誤是“系統找不到指定的文件路徑”。 文件路徑為templates / time.html。 time.html(我要呈現的頁面)的位置是

SRC /模板/為time.html

我的go main的位置是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)
}

問題是我的路徑不正確。

fp := path.Join("templates", "time.html")

fp := path.Join("Home/go/src/templates", "time.html")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM