My First Program

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

You requested this page on Sun Apr 11 23:41:25 EDT 2021


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