Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

template.Execute(): invalid memory address or nil pointer dereference

Tags:

go

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
like image 968
Sasan Rose Avatar asked Apr 13 '15 12:04

Sasan Rose


People also ask

What is a nil pointer dereference?

CWE-476: NULL Pointer Dereference: A NULL pointer dereference occurs when the application dereferences a pointer that it expects to be valid, but is NULL, typically causing a crash or exit.

What is invalid memory address?

Occurs when a read or write instruction references memory that is logically or physically invalid. If present, represents the location and associated call stack from which the memory block containing the offending address was allocated.


1 Answers

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).

like image 79
JimB Avatar answered Sep 29 '22 11:09

JimB