html/template
【Go】template.ExecuteTemplate中にエラーが起きて中途半端にレンダリングされてしまう問題
· ☕ 2 分で読めます
まずは問題のコードから package main import ( "html/template" "net/http" ) func exampleHandler(w http.ResponseWriter, r *http.Request) { t := template.Must(template.ParseFiles( "example.html", )) err := t.ExecuteTemplate(w, "example.html", nil) if err != nil { http.NotFound(w, r) return } } func main() { http.HandleFunc("/", exampleHandler) log.Fatalln(http.ListenAndServe(":8080", nil)) } 何が起きる? 上のコードは http.ListenAndServeでwebサーバーを立ち上げて、/にアクセスが来たらexample.htmlを返してあげるというシンプルなものです。 exampleHan