I am new to Go and am designing a website using Go. I wish to use multiple templates, such as a base template to incorporate with other templates , such as index. I would like to all the template parsing when the app first starts up. At the moment , I have base.html, footer.html and index.html. I wish to serve index.html which uses base.html and footer.html as well. At the moment , the only response I'm getting from the server is a single newline in a 200 HTTP response verified by wireshark. Anyways, here are my files:
main.go
package main
import (
"html/template"
"log"
"net/http"
)
type Initial struct {
Data string
}
var cached_templates = template.Must(template.ParseFiles("templates/base.html",
"templates/footer.html",
"templates/index.html"))
func renderInitialTemplate(w http.ResponseWriter, _template string, data *Initial) {
err := cached_templates.ExecuteTemplate(w, _template, data)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
func indexHandler(w http.ResponseWriter, r *http.Request) {
data := &Initial{Data: "Bob"}
renderInitialTemplate(w, "index.html", data)
}
func main() {
http.HandleFunc("/", indexHandler)
log.Fatal(http.ListenAndServe(":80", nil))
}
index.html - https://pastebin.com/LPy0Xb2Z
footer.html - https://pastebin.com/vVenX4qE
base.html - https://pastebin.com/1jKxv7Uz
I appreciate any help. Thanks.