Golang: error while open gtpl / html template

Today I just started my Golang web self training, I train my self a basic stuff regarding Golang web programming, I just got an issue that wasted my time for about 2 hours, a little bit frustrating for me because I’m sure I have followed the instruction in the right way, I type the code by my self then delete all of it and replace it by pasted code from the tutorial (the tutorial is here) but still got the same errors again and again:

This is the error that I got recently

2017/04/02 21:26:21 http: panic serving runtime error: invalid memory address or nil pointer dereference
goroutine 8 [running]:
 /usr/local/go/src/net/http/server.go:1491 +0x12a
panic(0x6d5ca0, 0xc42000c150)
 /usr/local/go/src/runtime/panic.go:458 +0x243
html/template.(*Template).escape(0x0, 0x0, 0x0)
 /usr/local/go/src/html/template/template.go:79 +0x44
html/template.(*Template).Execute(0x0, 0x8835c0, 0xc4201500d0, 0x0, 0x0, 0xc420140210, 0xc4201420e0)
 /usr/local/go/src/html/template/template.go:101 +0x2f
main.login(0x886fc0, 0xc4201500d0, 0xc4201083c0)
 /home/yusuf/go-learn2/src/web/main.go:29 +0x4a9
net/http.HandlerFunc.ServeHTTP(0x7546f8, 0x886fc0, 0xc4201500d0, 0xc4201083c0)
 /usr/local/go/src/net/http/server.go:1726 +0x44
net/http.(*ServeMux).ServeHTTP(0x8a6dc0, 0x886fc0, 0xc4201500d0, 0xc4201083c0)
 /usr/local/go/src/net/http/server.go:2022 +0x7f
net/http.serverHandler.ServeHTTP(0xc4200da200, 0x886fc0, 0xc4201500d0, 0xc4201083c0)
 /usr/local/go/src/net/http/server.go:2202 +0x7d
net/http.(*conn).serve(0xc4200da780, 0x887600, 0xc420016e40)
 /usr/local/go/src/net/http/server.go:1579 +0x4b7
created by net/http.(*Server).Serve
 /usr/local/go/src/net/http/server.go:2293 +0x44d



Weird error, but fortunately after 2 hours wasting of time I realize that the error is telling me about something wrong at following line:

/home/yusuf/go-learn2/src/web/main.go:29 +0x4a9

the code that contain a command to read the go template file ( login.gtpl ), the code is :

t, _ := template.ParseFiles("login.gtpl")
 t.Execute(w, nil)

then I replaced the path of file to the full path and then the code is working:

t, _ := template.ParseFiles("/my/full/path/login.gtpl")
t.Execute(w, nil)

and after that I set the code back to previous condition

t, _ := template.ParseFiles("login.gtpl")
t.Execute(w, nil)


and the code is still working, I don’t know why but it’s working now even without fullpath.

*if you still got an error regarding this file reader you could just give full access to the file using command below:

sudo chmod 777 /my/full/path/login.gtpl


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s