doudundian9558 2015-04-13 12:42
浏览 72
已采纳

template.Execute():无效的内存地址或nil指针取消引用

I am trying to use Go html/template with custom delimiters.

However, I can Parse and Execute my "index.html" file, whenever I am trying to change my templates Delimiters I face the following error:

runtime error: invalid memory address or nil pointer dereference goroutine

Here is my code:

package main

import (
        "html/template"
        "net/http"
)

var sherrifTmpl = template.New("test").Delims("{[{", "}]}")

func serveHome(w http.ResponseWriter, r *http.Request) {
        template.Must(sherrifTmpl.ParseFiles("index.html")).Execute(w, r)
}

If I try any of the followings:

package main

import (
        "html/template"
        "net/http"
)

func serveHome(w http.ResponseWriter, r *http.Request) {
        template.Must(template.ParseFiles("index.html")).Execute(w, r)
}

Or:

package main

import (
        "html/template"
        "net/http"
)

var sherrifTmpl = template.New("test").Delims("{[{", "}]}")

func serveHome(w http.ResponseWriter, r *http.Request) {
        template.Must(sherrifTmpl.ParseFiles("{[{.Host}]}")).Execute(w, r)
}

Everything works. I even tried to catch ParseFiles error. But still no luck:

package main

import (
        "html/template"
        "net/http"
)

var sherrifTmpl = template.New("test").Delims("{[{", "}]}")

func serveHome(w http.ResponseWriter, r *http.Request) {
        homeTmpl, err := sherrifTmpl.ParseFiles("index.html")
        if err != nil {
            panic(err)
        }
        homeTmpl.Execute(w, r)
}

I can't see where I am doing wrong. I would be grateful if any of you could help me on this issue.

Update 1:

Here is the panic:

2015/04/13 17:43:35 http: panic serving 127.0.0.1:56634: runtime error: invalid memory address or nil pointer dereference
goroutine 5 [running]:
net/http.func·011()
    /usr/local/go/src/net/http/server.go:1130 +0xbb
html/template.(*Template).escape(0xc20803ad80, 0x0, 0x0)
    /usr/local/go/src/html/template/template.go:59 +0xe4
html/template.(*Template).Execute(0xc20803ad80, 0x7f550e16f420, 0xc20805cd20, 0x7280c0, 0xc208032ea0, 0x0, 0x0)
    /usr/local/go/src/html/template/template.go:75 +0x3d
main.serveHome(0x7f550e16f328, 0xc20805cd20, 0xc208032ea0)
    /home/sasan/Works/Karina/Mobazi/Mon-Panel/routes.go:11 +0x136
net/http.HandlerFunc.ServeHTTP(0x7eb738, 0x7f550e16f328, 0xc20805cd20, 0xc208032ea0)
    /usr/local/go/src/net/http/server.go:1265 +0x41
net/http.(*ServeMux).ServeHTTP(0xc20803a6c0, 0x7f550e16f328, 0xc20805cd20, 0xc208032ea0)
    /usr/local/go/src/net/http/server.go:1541 +0x17d
net/http.serverHandler.ServeHTTP(0xc20805a0c0, 0x7f550e16f328, 0xc20805cd20, 0xc208032ea0)
    /usr/local/go/src/net/http/server.go:1703 +0x19a
net/http.(*conn).serve(0xc20805cc80)
    /usr/local/go/src/net/http/server.go:1204 +0xb57
created by net/http.(*Server).Serve
    /usr/local/go/src/net/http/server.go:1751 +0x35e
  • 写回答

1条回答 默认 最新

  • douao1858 2015-04-13 17:06
    关注

    A template may actually contain more than one "template" to execute. Here you actually have 2 templates; a nil template named "test" with custom delimiters, and a parsed template named "index.html".

    You can either name the first the same name as your index template

    var sherrifTmpl = template.New("index.html").Delims("{[{", "}]}")
    

    or you can call the template by name with ExecuteTemplate

    template.Must(sherrifTmpl.ParseFiles("index.html")).ExecuteTemplate(w, "index.html", r)
    

    The html/template package still shouldn't panic in this case. This is a bug that will be fixed in go1.5 (currently fixed in git master).

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

报告相同问题?

悬赏问题

  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试