Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

A webserver doesn't return any html page

I have this code, one html layout and its child:

package main

import (
  "net/http"
  "log"
  "html/template"
)

var indexTempl = template.Must(template.ParseFiles(
    "base.tmpl",
    "index.tmpl",
))

func main() {
  http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
    // w.Header().Set("Content-Type", "text/html; charset=utf-8")

    if err := indexTempl.ExecuteTemplate(w, "base.tmpl", nil); err != nil {
      log.Printf("Failed to execute base.tmpl: %v", err)
      // Send some error page, e.g.:
      http.Error(w, "base.tmpl error", http.StatusInternalServerError)
      // Return early if there are other stuff after the template execution
      return
    }

  })

  err := http.ListenAndServe(":4001", nil)
  if err != nil {
    log.Fatal("error with ListenAndServe: ", err)
  }
}

It works but a completely empty page gets shown when I visit localhost:4001.

It's not an empty html page

html pages

//base.tmpl

{{define "base"}}

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <title>
      {{block "title" .}}
      {{end}}
    </title>
  </head>

  <body>
    <div class="contant">
      {{ template "content" . }}
    </div>
  </body>
</html>
{{end}}



//index.tmpl

{{define "title"}}
  Main
{{end}}

{{define "content"}}

  test index 123

{{end}}

html pages html pages html pages html pages

Comments