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
homeTmpl.Execute(w, r)
. golang.org/pkg/html/template/#Template.Execute , play.golang.org/p/xpXFkaD-LB (Does not run, for illustrative purposes only) – Intermernet