Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
- }
- 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
- template: test:2: function "Title" not defined
- {{template "header" . Title}}
- {{template "header" .Title}}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement