This is the exact code copied from the tutorial. Im new in Go and web developing in general so im having a hard time debugging such error.
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)
}
}
There is also another file called login.gtpl with basic html codes
<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>
here is the error message
2017/07/23 18:41:29 http: panic serving 127.0.0.1:61254: runtime error: invalid
memory address or nil pointer dereference
goroutine 18 [running]:
net/http.(*conn).serve.func1(0xc04203adc0)
C:/Go/src/net/http/server.go:1721 +0xd7
panic(0x717940, 0x901950)
C:/Go/src/runtime/panic.go:489 +0x2dd
html/template.(*Template).escape(0x0, 0x0, 0x0)
C:/Go/src/html/template/template.go:94 +0x3f
html/template.(*Template).Execute(0x0, 0x8da500, 0xc0420fa0e0, 0x0, 0x0, 0xc0420
08390, 0x90e480)
C:/Go/src/html/template/template.go:117 +0x36
main.login(0x8de180, 0xc0420fa0e0, 0xc042030400)
D:/sample/src/github.com/sunilrajramchandani/astaxie/web.go:29 +0x47e
net/http.HandlerFunc.ServeHTTP(0x786e68, 0x8de180, 0xc0420fa0e0, 0xc042030400)
C:/Go/src/net/http/server.go:1942 +0x4b
net/http.(*ServeMux).ServeHTTP(0x90e480, 0x8de180, 0xc0420fa0e0, 0xc042030400)
C:/Go/src/net/http/server.go:2238 +0x137
net/http.serverHandler.ServeHTTP(0xc04207ab00, 0x8de180, 0xc0420fa0e0, 0xc042030
400)
C:/Go/src/net/http/server.go:2568 +0x99
net/http.(*conn).serve(0xc04203adc0, 0x8de740, 0xc04203e3c0)
C:/Go/src/net/http/server.go:1825 +0x619
created by net/http.(*Server).Serve
C:/Go/src/net/http/server.go:2668 +0x2d5
method: GET
2017/07/23 18:41:29 http: panic serving 127.0.0.1:61255: runtime error: invalid
memory address or nil pointer dereference
goroutine 4 [running]:
net/http.(*conn).serve.func1(0xc0420f0000)
C:/Go/src/net/http/server.go:1721 +0xd7
panic(0x717940, 0x901950)
C:/Go/src/runtime/panic.go:489 +0x2dd
html/template.(*Template).escape(0x0, 0x0, 0x0)
C:/Go/src/html/template/template.go:94 +0x3f
html/template.(*Template).Execute(0x0, 0x8da500, 0xc04213e000, 0x0, 0x0, 0xc0421
36090, 0x90e480)
C:/Go/src/html/template/template.go:117 +0x36
main.login(0x8de180, 0xc04213e000, 0xc042030500)
D:/sample/src/github.com/sunilrajramchandani/astaxie/web.go:29 +0x47e
net/http.HandlerFunc.ServeHTTP(0x786e68, 0x8de180, 0xc04213e000, 0xc042030500)
C:/Go/src/net/http/server.go:1942 +0x4b
net/http.(*ServeMux).ServeHTTP(0x90e480, 0x8de180, 0xc04213e000, 0xc042030500)
C:/Go/src/net/http/server.go:2238 +0x137
net/http.serverHandler.ServeHTTP(0xc04207ab00, 0x8de180, 0xc04213e000, 0xc042030
500)
C:/Go/src/net/http/server.go:2568 +0x99
net/http.(*conn).serve(0xc0420f0000, 0x8de740, 0xc04203e500)
C:/Go/src/net/http/server.go:1825 +0x619
created by net/http.(*Server).Serve
C:/Go/src/net/http/server.go:2668 +0x2d5
method: GET
2017/07/23 18:41:29 http: panic serving 127.0.0.1:61257: runtime error: invalid
memory address or nil pointer dereference
goroutine 6 [running]:
net/http.(*conn).serve.func1(0xc0420f00a0)
C:/Go/src/net/http/server.go:1721 +0xd7
panic(0x717940, 0x901950)
C:/Go/src/runtime/panic.go:489 +0x2dd
html/template.(*Template).escape(0x0, 0x0, 0x0)
C:/Go/src/html/template/template.go:94 +0x3f
html/template.(*Template).Execute(0x0, 0x8da500, 0xc0420fa2a0, 0x0, 0x0, 0xc0420
084b0, 0x90e480)
C:/Go/src/html/template/template.go:117 +0x36
main.login(0x8de180, 0xc0420fa2a0, 0xc042162000)
D:/sample/src/github.com/sunilrajramchandani/astaxie/web.go:29 +0x47e
net/http.HandlerFunc.ServeHTTP(0x786e68, 0x8de180, 0xc0420fa2a0, 0xc042162000)
C:/Go/src/net/http/server.go:1942 +0x4b
net/http.(*ServeMux).ServeHTTP(0x90e480, 0x8de180, 0xc0420fa2a0, 0xc042162000)
C:/Go/src/net/http/server.go:2238 +0x137
net/http.serverHandler.ServeHTTP(0xc04207ab00, 0x8de180, 0xc0420fa2a0, 0xc042162
000)
C:/Go/src/net/http/server.go:2568 +0x99
net/http.(*conn).serve(0xc0420f00a0, 0x8de740, 0xc04210c140)
C:/Go/src/net/http/server.go:1825 +0x619
created by net/http.(*Server).Serve
C:/Go/src/net/http/server.go:2668 +0x2d5
method: GET
2017/07/23 18:41:29 http: panic serving 127.0.0.1:61259: runtime error: invalid
memory address or nil pointer dereference
goroutine 8 [running]:
net/http.(*conn).serve.func1(0xc0420f0140)
C:/Go/src/net/http/server.go:1721 +0xd7
panic(0x717940, 0x901950)
C:/Go/src/runtime/panic.go:489 +0x2dd
html/template.(*Template).escape(0x0, 0x0, 0x0)
C:/Go/src/html/template/template.go:94 +0x3f
html/template.(*Template).Execute(0x0, 0x8da500, 0xc0420ec1c0, 0x0, 0x0, 0xc0420
62f30, 0x90e480)
C:/Go/src/html/template/template.go:117 +0x36
main.login(0x8de180, 0xc0420ec1c0, 0xc042000400)
D:/sample/src/github.com/sunilrajramchandani/astaxie/web.go:29 +0x47e
net/http.HandlerFunc.ServeHTTP(0x786e68, 0x8de180, 0xc0420ec1c0, 0xc042000400)
C:/Go/src/net/http/server.go:1942 +0x4b
net/http.(*ServeMux).ServeHTTP(0x90e480, 0x8de180, 0xc0420ec1c0, 0xc042000400)
C:/Go/src/net/http/server.go:2238 +0x137
net/http.serverHandler.ServeHTTP(0xc04207ab00, 0x8de180, 0xc0420ec1c0, 0xc042000
400)
C:/Go/src/net/http/server.go:2568 +0x99
net/http.(*conn).serve(0xc0420f0140, 0x8de740, 0xc04200a5c0)
C:/Go/src/net/http/server.go:1825 +0x619
created by net/http.(*Server).Serve
C:/Go/src/net/http/server.go:2668 +0x2d5
method: GET
2017/07/23 18:41:34 http: panic serving 127.0.0.1:61264: runtime error: invalid
memory address or nil pointer dereference
goroutine 34 [running]:
net/http.(*conn).serve.func1(0xc042198000)
C:/Go/src/net/http/server.go:1721 +0xd7
panic(0x717940, 0x901950)
C:/Go/src/runtime/panic.go:489 +0x2dd
html/template.(*Template).escape(0x0, 0x0, 0x0)
C:/Go/src/html/template/template.go:94 +0x3f
html/template.(*Template).Execute(0x0, 0x8da500, 0xc04213e1c0, 0x0, 0x0, 0xc0421
361b0, 0x90e480)
C:/Go/src/html/template/template.go:117 +0x36
main.login(0x8de180, 0xc04213e1c0, 0xc042030600)
D:/sample/src/github.com/sunilrajramchandani/astaxie/web.go:29 +0x47e
net/http.HandlerFunc.ServeHTTP(0x786e68, 0x8de180, 0xc04213e1c0, 0xc042030600)
C:/Go/src/net/http/server.go:1942 +0x4b
net/http.(*ServeMux).ServeHTTP(0x90e480, 0x8de180, 0xc04213e1c0, 0xc042030600)
C:/Go/src/net/http/server.go:2238 +0x137
net/http.serverHandler.ServeHTTP(0xc04207ab00, 0x8de180, 0xc04213e1c0, 0xc042030
600)
C:/Go/src/net/http/server.go:2568 +0x99
net/http.(*conn).serve(0xc042198000, 0x8de740, 0xc04203e780)
C:/Go/src/net/http/server.go:1825 +0x619
created by net/http.(*Server).Serve
C:/Go/src/net/http/server.go:2668 +0x2d5
method: GET
2017/07/23 18:42:04 http: panic serving 127.0.0.1:61279: runtime error: invalid
memory address or nil pointer dereference
goroutine 36 [running]:
net/http.(*conn).serve.func1(0xc0421980a0)
C:/Go/src/net/http/server.go:1721 +0xd7
panic(0x717940, 0x901950)
C:/Go/src/runtime/panic.go:489 +0x2dd
html/template.(*Template).escape(0x0, 0x0, 0x0)
C:/Go/src/html/template/template.go:94 +0x3f
html/template.(*Template).Execute(0x0, 0x8da500, 0xc0420fa460, 0x0, 0x0, 0xc0420
085d0, 0x90e480)
C:/Go/src/html/template/template.go:117 +0x36
main.login(0x8de180, 0xc0420fa460, 0xc042162100)
D:/sample/src/github.com/sunilrajramchandani/astaxie/web.go:29 +0x47e
net/http.HandlerFunc.ServeHTTP(0x786e68, 0x8de180, 0xc0420fa460, 0xc042162100)
C:/Go/src/net/http/server.go:1942 +0x4b
net/http.(*ServeMux).ServeHTTP(0x90e480, 0x8de180, 0xc0420fa460, 0xc042162100)
C:/Go/src/net/http/server.go:2238 +0x137
net/http.serverHandler.ServeHTTP(0xc04207ab00, 0x8de180, 0xc0420fa460, 0xc042162
100)
C:/Go/src/net/http/server.go:2568 +0x99
net/http.(*conn).serve(0xc0421980a0, 0x8de740, 0xc04210c3c0)
C:/Go/src/net/http/server.go:1825 +0x619
created by net/http.(*Server).Serve
C:/Go/src/net/http/server.go:2668 +0x2d5
method: GET
2017/07/23 18:43:04 http: panic serving 127.0.0.1:61329: runtime error: invalid
memory address or nil pointer dereference
goroutine 10 [running]:
net/http.(*conn).serve.func1(0xc0420f01e0)
C:/Go/src/net/http/server.go:1721 +0xd7
panic(0x717940, 0x901950)
C:/Go/src/runtime/panic.go:489 +0x2dd
html/template.(*Template).escape(0x0, 0x0, 0x0)
C:/Go/src/html/template/template.go:94 +0x3f
html/template.(*Template).Execute(0x0, 0x8da500, 0xc04213e380, 0x0, 0x0, 0xc0421
362d0, 0x90e480)
C:/Go/src/html/template/template.go:117 +0x36
main.login(0x8de180, 0xc04213e380, 0xc042030700)
D:/sample/src/github.com/sunilrajramchandani/astaxie/web.go:29 +0x47e
net/http.HandlerFunc.ServeHTTP(0x786e68, 0x8de180, 0xc04213e380, 0xc042030700)
C:/Go/src/net/http/server.go:1942 +0x4b
net/http.(*ServeMux).ServeHTTP(0x90e480, 0x8de180, 0xc04213e380, 0xc042030700)
C:/Go/src/net/http/server.go:2238 +0x137
net/http.serverHandler.ServeHTTP(0xc04207ab00, 0x8de180, 0xc04213e380, 0xc042030
700)
C:/Go/src/net/http/server.go:2568 +0x99
net/http.(*conn).serve(0xc0420f01e0, 0x8de740, 0xc04203e880)
C:/Go/src/net/http/server.go:1825 +0x619
created by net/http.(*Server).Serve
C:/Go/src/net/http/server.go:2668 +0x2d5