I can't figure out this error in go lang when executing a template
panic: open templates/*.html: The system cannot find the path specified.
another problem is that my public folder can't be served from css and I don't know why.
code:
package main
import (
"net/http"
"github.com/gorilla/mux"
"html/template"
"log"
)
var tpl *template.Template
func init() {
tpl = template.Must(template.ParseGlob("templates/*.html"))
}
func main() {
r := mux.NewRouter()
r.HandleFunc("/",home)
http.Handle("/public/", http.StripPrefix("/public/",
http.FileServer(http.Dir("/pub"))))
http.ListenAndServe(":8080", r)
}
func home(writer http.ResponseWriter, request *http.Request) {
err := tpl.ExecuteTemplate(writer, "index.html", nil)
if err != nil {
log.Println(err)
http.Error(writer, "Internal server error", http.StatusInternalServerError)
}
}
my folder is like that:
bin pkg pub css home.css js src github.com gorila/mux templates index.html
my GOROOT is pointing to the folder project
which contains the bin
pkg
src
when I make the templates
folder outside src
it works fine but I thinks that's not right everything must be in src
right ?
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>test</title>
<link href="../public/css/home.css" type="text/css" rel="stylesheet" />
</head>
<body>
<h1>welcome! hi</h1>
</body>
</html>
here i can't serve the css
UPDATE::
the first problem is solved of the templates folder .
but I am still cannot solve the second issue