-3
func Home(w http.ResponseWriter, r *staticAuth.AuthenticatedRequest) {
    t, err := template.ParseFiles("index.html") //parse the html file homepage.html
    if err != nil {                             // if there is an error
        log.Print("template parsing error: ", err) // log it
    }
    err = t.Execute(w, nil) //execute the template and pass it the HomePageVars struct to fill in the gaps
    if err != nil {         // if there is an error
        log.Print("template executing error: ", err) //log it
    }
}
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
  • 2
    Please go through - How do I ask a good [question](https://stackoverflow.com/help/how-to-ask) ? – Himanshu Feb 19 '18 at 11:13
  • 1
    You shouldn't have handlers that parse templates. See: [It takes too much time when using “template” package to generate a dynamic web page to client in golang](https://stackoverflow.com/questions/28451675/it-takes-too-much-time-when-using-template-package-to-generate-a-dynamic-web-p/28453523#28453523). Once you move the template parsing out of the handler, it also becomes much easier to test. – icza Feb 19 '18 at 11:16
  • A good unit test tests all meaningful input values, and that they produce all meaningful output values. Input includes global state, such as global variables, databases, or the filesystem. Output includes side effects (such as modifying global variables, changing database entries, or writing to the filesystem). Given that: What are the meaningful inputs for this function? And given those inputs, what should the outputs be? Write tests that cover these cases. – Jonathan Hall Feb 19 '18 at 11:21

1 Answers1

1
func Home(w http.ResponseWriter, r *staticAuth.AuthenticatedRequest) {
    t, err := template.ParseFiles("index.html") //parse the html file homepage.html
    if err != nil {                             // if there is an error
        log.Print("template parsing error: ", err) // log it
    }
    err = t.Execute(w, nil) //execute the template and pass it the HomePageVars struct to fill in the gaps
    if err != nil {         // if there is an error
        log.Print("template executing error: ", err) //log it
    }
}

In your code, you check for and log errors, then you keep going and ignore the errors.

peterSO
  • 158,998
  • 31
  • 281
  • 276