I'm getting this error in a very simple golang web application.

619 views
Skip to first unread message

baraval...@gmail.com

unread,
Aug 19, 2014, 4:27:07 PM8/19/14
to golan...@googlegroups.com
Hi guys I'm tring to learn go and I have build a very simple web application but I'm getting an error and I don't know how to solve it.

THis is the code:  
package main

import (
"fmt"
"html/template"
"log"
"net/http"
"strings"
)

func sayhelloName(w http.ResponseWriter, r *http.Request) {
r.ParseForm()  //Parse url parameters passed, then parse the response packet for the POST body (request body)
// attention: If you do not call ParseForm method, the following data can not be obtained form
fmt.Println(r.Form) // print information on server side.
fmt.Println("path", r.URL.Path)
fmt.Println("scheme", r.URL.Scheme)
fmt.Println(r.Form["url_long"])
for k, v := range r.Form {
fmt.Println("key:", k)
fmt.Println("val:", strings.Join(v, ""))
}
fmt.Fprintf(w, "Hello astaxie!") // write data to response
}

func login(w http.ResponseWriter, r *http.Request) {
fmt.Println("method:", r.Method) //get request method
if r.Method == "GET" {
t, _ := template.ParseFiles("login.gtpl")
t.Execute(w, nil)
} else {
r.ParseForm()
// logic part of log in
fmt.Println("username:", r.Form["username"])
fmt.Println("password:", r.Form["password"])
}
}

func main() {
http.HandleFunc("/", sayhelloName) // setting router rule
http.HandleFunc("/login", login)
err := http.ListenAndServe(":9090", nil) // setting listening port
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}

This is login.gtpl
<html>
<head>
    <title></title>
</head>
<body>
<form action="/login" method="post">
    Username:<input type="text" name="username">
    Password:<input type="password" name="password">
    <input type="submit" value="Login">
</form>
</body>
</html>
And this is the error:  
C:/Go/bin/go.exe run C:/Users/Fabio/golangWorkspace/SimpleInputForm/src/SimpleInputForm.go
2014/08/19 22:21:40 http: panic serving 127.0.0.1:13105: runtime error: invalid memory address or nil pointer dereference
goroutine 20 [running]:
method: GET
net/http.func·011()
c:/go/src/pkg/net/http/server.go:1100 +0xbe
runtime.panic(0x6dea60, 0x8e15a2)
c:/go/src/pkg/runtime/panic.c:248 +0x1d3
html/template.(*Template).escape(0x0, 0x0, 0x0)
c:/go/src/pkg/html/template/template.go:52 +0x43
method: GET
html/template.(*Template).Execute(0x0, 0x351908, 0xc08203ab40, 0x0, 0x0, 0x0, 0x0)
c:/go/src/pkg/html/template/template.go:70 +0x48
main.login(0x351880, 0xc08203ab40, 0xc08201c750)
C:/Users/Fabio/golangWorkspace/SimpleInputForm/src/SimpleInputForm.go:29 +0x227
net/http.HandlerFunc.ServeHTTP(0x7c6678, 0x351880, 0xc08203ab40, 0xc08201c750)
c:/go/src/pkg/net/http/server.go:1235 +0x47
net/http.(*ServeMux).ServeHTTP(0xc082044030, 0x351880, 0xc08203ab40, 0xc08201c750)
c:/go/src/pkg/net/http/server.go:1511 +0x1aa
net/http.serverHandler.ServeHTTP(0xc08200c420, 0x351880, 0xc08203ab40, 0xc08201c750)
c:/go/src/pkg/net/http/server.go:1673 +0x1a6
net/http.(*conn).serve(0xc082046000)
c:/go/src/pkg/net/http/server.go:1174 +0xa85
created by net/http.(*Server).Serve
c:/go/src/pkg/net/http/server.go:1721 +0x31a
2014/08/19 22:21:40 http: panic serving 127.0.0.1:13106: runtime error: invalid memory address or nil pointer dereference
goroutine 21 [running]:
net/http.func·011()
c:/go/src/pkg/net/http/server.go:1100 +0xbe
runtime.panic(0x6dea60, 0x8e15a2)
c:/go/src/pkg/runtime/panic.c:248 +0x1d3
html/template.(*Template).escape(0x0, 0x0, 0x0)
c:/go/src/pkg/html/template/template.go:52 +0x43
html/template.(*Template).Execute(0x0, 0x351908, 0xc08203a0a0, 0x0, 0x0, 0x0, 0x0)
c:/go/src/pkg/html/template/template.go:70 +0x48
main.login(0x351880, 0xc08203a0a0, 0xc08201c820)
C:/Users/Fabio/golangWorkspace/SimpleInputForm/src/SimpleInputForm.go:29 +0x227
net/http.HandlerFunc.ServeHTTP(0x7c6678, 0x351880, 0xc08203a0a0, 0xc08201c820)
c:/go/src/pkg/net/http/server.go:1235 +0x47
net/http.(*ServeMux).ServeHTTP(0xc082044030, 0x351880, 0xc08203a0a0, 0xc08201c820)
c:/go/src/pkg/net/http/server.go:1511 +0x1aa
net/http.serverHandler.ServeHTTP(0xc08200c420, 0x351880, 0xc08203a0a0, 0xc08201c820)
c:/go/src/pkg/net/http/server.go:1673 +0x1a6
net/http.(*conn).serve(0xc082046200)
c:/go/src/pkg/net/http/server.go:1174 +0xa85
created by net/http.(*Server).Serve
c:/go/src/pkg/net/http/server.go:1721 +0x31a
method: GET
2014/08/19 22:21:40 http: panic serving 127.0.0.1:13107: runtime error: invalid memory address or nil pointer dereference
goroutine 22 [running]:
net/http.func·011()
c:/go/src/pkg/net/http/server.go:1100 +0xbe
runtime.panic(0x6dea60, 0x8e15a2)
c:/go/src/pkg/runtime/panic.c:248 +0x1d3
html/template.(*Template).escape(0x0, 0x0, 0x0)
c:/go/src/pkg/html/template/template.go:52 +0x43
html/template.(*Template).Execute(0x0, 0x351908, 0xc08203a140, 0x0, 0x0, 0x0, 0x0)
c:/go/src/pkg/html/template/template.go:70 +0x48
main.login(0x351880, 0xc08203a140, 0xc08201c410)
C:/Users/Fabio/golangWorkspace/SimpleInputForm/src/SimpleInputForm.go:29 +0x227
net/http.HandlerFunc.ServeHTTP(0x7c6678, 0x351880, 0xc08203a140, 0xc08201c410)
c:/go/src/pkg/net/http/server.go:1235 +0x47
net/http.(*ServeMux).ServeHTTP(0xc082044030, 0x351880, 0xc08203a140, 0xc08201c410)
c:/go/src/pkg/net/http/server.go:1511 +0x1aa
net/http.serverHandler.ServeHTTP(0xc08200c420, 0x351880, 0xc08203a140, 0xc08201c410)
c:/go/src/pkg/net/http/server.go:1673 +0x1a6
net/http.(*conn).serve(0xc082047700)
c:/go/src/pkg/net/http/server.go:1174 +0xa85
created by net/http.(*Server).Serve
c:/go/src/pkg/net/http/server.go:1721 +0x31a



These are the path:  
GOPATH: C:\Users\Fabio\golang\golangWorkspace
GOROOT: C:\Go\
SimpleInputForm.go: C:\Users\Fabio\golangWorkSpace\SimpleInputForm\src\
SimpleInputForm.go
login.gptl: 
C:\Users\Fabio\golangWorkSpace\SimpleInputForm\src\login.gptl



Dave Cheney

unread,
Aug 19, 2014, 5:50:47 PM8/19/14
to golan...@googlegroups.com, baraval...@gmail.com
Handle the error from ParseForm and it will tell you why it couldn't parse your template.

Matt Silverlock

unread,
Aug 19, 2014, 7:43:07 PM8/19/14
to golan...@googlegroups.com, baraval...@gmail.com
C:/Users/Fabio/golangWorkspace/SimpleInputForm/src/SimpleInputForm.go:29 +0x227
net/http.HandlerFunc.ServeHTTP(0x7c6678, 0x351880, 0xc08203a0a0, 0xc08201c820)

... appears to correspond to "t.Execute(w, nil)" (noting that this returns an error). Which is tied into the fact that your template parsing is probably failing (does the file exist in the same directory as your Go application is running from?)

I also believe Dave meant to write "handle the error from ParseFiles", but you're ignoring the errors from both that and r.ParseForm so check both.
Reply all
Reply to author
Forward
0 new messages