-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{{ define "content" }} | |
<h1 style="color: blue;">Hello World!</h1> | |
{{ end }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{{ 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 }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{{ 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