Skip to content

Instantly share code, notes, and snippets.

@lozandier
Created May 28, 2015 11:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lozandier/a91e7ea5439316bb5515 to your computer and use it in GitHub Desktop.
Save lozandier/a91e7ea5439316bb5515 to your computer and use it in GitHub Desktop.
Unexpected "invalid memory address error or nil pointer dereference error" when executing Listing 5.29
{{ define "content" }}
<h1 style="color: blue;">Hello World!</h1>
{{ end }}
{{ define "layout" }}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
{{ template "content" }}
</body>
</html>
{{ end }}
{{ define "content" }}
Hello World!
{{ end }}
package main
import (
"html/template"
"math/rand"
"net/http"
"time"
)
func process(writer http.ResponseWriter, r *http.Request) {
rand.Seed(time.Now().Unix())
var t *template.Template
if rand.Intn(10) > 5 {
t, _ = template.ParseFiles("layout.html", "red_hello.html")
} else {
t, _ = template.ParseFiles("layout.html", "blue_hello.html")
}
t.ExecuteTemplate(writer, "layout.html", "")
}
func main() {
server := http.Server{
Addr: "127.0.0.1:8080",
}
http.HandleFunc("/process", process)
server.ListenAndServe()
}
{{ define "content" }}
<h1 style="color: red;">Hello World!</h1>
{{ end }}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment