简体   繁体   中英

Using GoLang templates: System cannot find path specified

Hello I'm just getting started learning about html/templates in golang. The error i'm getting is that 'system cannot find file path specified'. and the file path is templates/time.html. the location of time.html (the page i'm trying to render) is

src/templates/time.html

the location of my go main is src/timeserver/timerserver.go

here's the code i used

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)
}

Problem was that my path wasn't correct. changed

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

to

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM