这是我的代码:
m.Get("/", func(r render.Render) string {
t := template.New("some template")
toto := "titi"
templateh := "<html>Hello world! {{ toto }} <form name='input' action='../first' method='post' ><input type='texte' name='toto'><input type='submit' value='Submit'></form></html>"
t, _ = t.Parse(templateh)
var doc bytes.Buffer
err := t.Execute(&doc, toto)
if err != nil {
fmt.Println("There was an error:", err)
}
s := doc.String()
fmt.Println(s)
return s
})
它返回一个运行时错误:无效的内存地址或零指针取消引用
我不明白为什么...
最佳答案
电话
t, _ = t.Parse(templateh)
返回 nil 和错误,指出函数“todo”未定义。模板 Execute 方法取消引用 nil 指针,导致 panic 。
你应该改变两件事:
- 检查并处理解析调用返回的错误。这是使用 template.Must 的好地方辅助功能。
- 通过将
{{ todo }}
替换为{{.}}
来修复模板>
关于go - 运行时错误 : invalid memory address or nil pointer dereference in martini with template,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25858592/