douluo3256 2015-07-01 12:05
浏览 300
已采纳

遵循Pluralsight Go教程时出现无效的内存地址错误

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.

  • 写回答

1条回答 默认 最新

  • douhuan5073 2015-07-01 12:41
    关注

    You're ignoring errors returned by Parse. If you didn't, you'd see this:

    template: test:2: function "Title" not defined
    

    Your template is incorrect, so Lookup("test") returns nil.

    {{template "header" . Title}}
    

    should be

    {{template "header" .Title}}
    

    No space after the dot.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!