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 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂