I am following a tutorial from Pluralsight and doing exactly what it says, and the compilation even works, but when refreshing the page on the browser the application panics and outputs error on the console and the http server does not work as expected.
The src code that generates this error is as follows:
package main
import (
"net/http"
"text/template"
)
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
w.Header().Add("Content Type", "text/html")
templates := template.New("template")
templates.New("test").Parse(doc)
templates.New("header").Parse(header)
templates.New("footer").Parse(footer)
context := Context{
[3]string{"Lemon", "Orange", "Apple"},
"the title",
}
templates.Lookup("test").Execute(w, context)
})
http.ListenAndServe(":80", nil)
}
const doc = `
{{template "header" . Title}}
<body>
<h1>List of Fruit</h1>
<ul>
{{range .Fruit}}
<li>{{.}}</li>
{{end}}
</ul>
</body>
`
const header = `
<!DOCTYPE html>
<html>
<head><title>{{.}}</title></head>
`
const footer = `
</html>
`
type Context struct {
Fruit [3]string
Title string
}
and the error messages from the console
2015/07/01 13:53:14 http: panic serving 127.0.0.1:56994: runtime error: invalid memory address or nil pointer dereference
goroutine 5 [running]:
net/http.func·011()
c:/go/src/net/http/server.go:1130 +0xc2
text/template.errRecover(0xc082097b48)
c:/go/src/text/template/exec.go:100 +0xc3
text/template.(*Template).Execute(0x0, 0x353378, 0xc082070280, 0x6a5000, 0xc082008840, 0x0, 0x0)
c:/go/src/text/template/exec.go:137 +0x249
main.func·001(0x353210, 0xc082070280, 0xc082020000)
D:/Go/src/main/main.go:19 +0x325
net/http.HandlerFunc.ServeHTTP(0x786560, 0x353210, 0xc082070280, 0xc082020000)
c:/go/src/net/http/server.go:1265 +0x48
net/http.(*ServeMux).ServeHTTP(0xc08205c360, 0x353210, 0xc082070280, 0xc082020000)
c:/go/src/net/http/server.go:1541 +0x184
net/http.serverHandler.ServeHTTP(0xc082044180, 0x353210, 0xc082070280, 0xc082020000)
c:/go/src/net/http/server.go:1703 +0x1a1
net/http.(*conn).serve(0xc082070000)
c:/go/src/net/http/server.go:1204 +0xb5e
created by net/http.(*Server).Serve
c:/go/src/net/http/server.go:1751 +0x365
2015/07/01 13:53:14 http: panic serving 127.0.0.1:56995: runtime error: invalid memory address or nil pointer dereference
goroutine 6 [running]:
net/http.func·011()
c:/go/src/net/http/server.go:1130 +0xc2
text/template.errRecover(0xc082067b48)
c:/go/src/text/template/exec.go:100 +0xc3
text/template.(*Template).Execute(0x0, 0x353378, 0xc082070320, 0x6a5000, 0xc082008a40, 0x0, 0x0)
c:/go/src/text/template/exec.go:137 +0x249
main.func·001(0x353210, 0xc082070320, 0xc0820200d0)
D:/Go/src/main/main.go:19 +0x325
net/http.HandlerFunc.ServeHTTP(0x786560, 0x353210, 0xc082070320, 0xc0820200d0)
c:/go/src/net/http/server.go:1265 +0x48
net/http.(*ServeMux).ServeHTTP(0xc08205c360, 0x353210, 0xc082070320, 0xc0820200d0)
c:/go/src/net/http/server.go:1541 +0x184
net/http.serverHandler.ServeHTTP(0xc082044180, 0x353210, 0xc082070320, 0xc0820200d0)
c:/go/src/net/http/server.go:1703 +0x1a1
net/http.(*conn).serve(0xc0820701e0)
c:/go/src/net/http/server.go:1204 +0xb5e
created by net/http.(*Server).Serve
c:/go/src/net/http/server.go:1751 +0x365
2015/07/01 13:53:14 http: panic serving 127.0.0.1:56997: runtime error: invalid memory address or nil pointer dereference
goroutine 13 [running]:
net/http.func·011()
c:/go/src/net/http/server.go:1130 +0xc2
text/template.errRecover(0xc08201fb48)
c:/go/src/text/template/exec.go:100 +0xc3
text/template.(*Template).Execute(0x0, 0x353378, 0xc082070460, 0x6a5000, 0xc082008dc0, 0x0, 0x0)
c:/go/src/text/template/exec.go:137 +0x249
main.func·001(0x353210, 0xc082070460, 0xc0820209c0)
D:/Go/src/main/main.go:19 +0x325
net/http.HandlerFunc.ServeHTTP(0x786560, 0x353210, 0xc082070460, 0xc0820209c0)
c:/go/src/net/http/server.go:1265 +0x48
net/http.(*ServeMux).ServeHTTP(0xc08205c360, 0x353210, 0xc082070460, 0xc0820209c0)
c:/go/src/net/http/server.go:1541 +0x184
net/http.serverHandler.ServeHTTP(0xc082044180, 0x353210, 0xc082070460, 0xc0820209c0)
c:/go/src/net/http/server.go:1703 +0x1a1
net/http.(*conn).serve(0xc0820703c0)
c:/go/src/net/http/server.go:1204 +0xb5e
created by net/http.(*Server).Serve
c:/go/src/net/http/server.go:1751 +0x365
2015/07/01 13:53:15 http: panic serving 127.0.0.1:56998: runtime error: invalid memory address or nil pointer dereference
goroutine 17 [running]:
net/http.func·011()
c:/go/src/net/http/server.go:1130 +0xc2
text/template.errRecover(0xc082067b48)
c:/go/src/text/template/exec.go:100 +0xc3
text/template.(*Template).Execute(0x0, 0x353378, 0xc0820701e0, 0x6a5000, 0xc082008840, 0x0, 0x0)
c:/go/src/text/template/exec.go:137 +0x249
main.func·001(0x353210, 0xc0820701e0, 0xc0820200d0)
D:/Go/src/main/main.go:19 +0x325
net/http.HandlerFunc.ServeHTTP(0x786560, 0x353210, 0xc0820701e0, 0xc0820200d0)
c:/go/src/net/http/server.go:1265 +0x48
net/http.(*ServeMux).ServeHTTP(0xc08205c360, 0x353210, 0xc0820701e0, 0xc0820200d0)
c:/go/src/net/http/server.go:1541 +0x184
net/http.serverHandler.ServeHTTP(0xc082044180, 0x353210, 0xc0820701e0, 0xc0820200d0)
c:/go/src/net/http/server.go:1703 +0x1a1
net/http.(*conn).serve(0xc082070000)
c:/go/src/net/http/server.go:1204 +0xb5e
created by net/http.(*Server).Serve
c:/go/src/net/http/server.go:1751 +0x365
Don't know what to do here. Makes me feel a bit uneasy.