I have a simple main.go script that loads a template from a folder. The template looks like:
<html>
<head>
<title>T2 template</title>
</head>
<body>
hello
</body>
</html>
The main.go script looks is:
package main
import (
"fmt"
"html/template"
"log"
"net/http"
"os"
"github.com/gorilla/mux"
)
var (
templates = template.Must(template.ParseFiles("templates/index.html"))
)
func main() {
port := os.Getenv("PORT")
fmt.Printf("port is: %v
", port)
r := mux.NewRouter()
r.HandleFunc("/hello", HomeHandler)
log.Fatal(http.ListenAndServe(":"+port, r))
}
func HomeHandler(w http.ResponseWriter, r *http.Request) {
tmpl := "templates/index.html"
err := templates.ExecuteTemplate(w, tmpl, "")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
I'm not sure what is wrong here.
The error I see in the browser is:
"templates/index.html" is undefined