home

My First Program

Exercise 2 in Chapter 1 Basics of Software Engineering for Internet Applications.

You requested this page on Mon May 10 21:01:35 EDT 2021

Tada!

Lesson learned: if your template rendering has an error, the idiom presented in Writing Web Applications does not handle this well, as it interleaves a partial template render with the error message. It also returns a status of 200.

The fix I came up with is to use a buffer:

		log.Printf("executing template %s\n", fn)
		var buf bytes.Buffer
		data := map[string]string {"Now":time.Now().Format(time.UnixDate)}
		err = t.Execute(&buf, data)
		if err != nil {
			log.Printf("%s: %s\n", fn, err.Error())
			http.Error(w, "Internal Server Error", http.StatusInternalServerError)
		} else {
			fmt.Fprint(w, &buf)
		}
  

-- Mark Bucciarelli, April 2, 2021