Helllo everyone I am newbie in GoLang. I am getting the error (heading)
for my below code. please help me, I am attaching my login.html file also.
I am not getting what is wrong in my 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.html")
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)
}
}
Here is my html file
<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 the error that I am getting.
when trying http://127.0.0.1:9090/login
method: GET
2020/01/02 17:31:25 http: panic serving 127.0.0.1:34910: runtime error: invalid memory address or nil pointer dereference
goroutine 62 [running]:
net/http.(*conn).serve.func1(0xc0000b1ea0)
/snap/go/4901/src/net/http/server.go:1767 +0x139
panic(0x78de40, 0xae4fe0)
/snap/go/4901/src/runtime/panic.go:679 +0x1b2
html/template.(*Template).escape(0x0, 0x0, 0x0)
/snap/go/4901/src/html/template/template.go:95 +0x42
html/template.(*Template).Execute(0x0, 0x87b880, 0xc000130b60, 0x0, 0x0, 0x87b9c0, 0xc0001171a0)
/snap/go/4901/src/html/template/template.go:119 +0x2f
main.login(0x881680, 0xc000130b60, 0xc000118b00)
/home/mmt8037/go/src/awesomeProject1/main/goWeb.go:29 +0x357
net/http.HandlerFunc.ServeHTTP(0x815428, 0x881680, 0xc000130b60, 0xc000118b00)
/snap/go/4901/src/net/http/server.go:2007 +0x44
net/http.(*ServeMux).ServeHTTP(0xaf4e40, 0x881680, 0xc000130b60, 0xc000118b00)
/snap/go/4901/src/net/http/server.go:2387 +0x1bd
net/http.serverHandler.ServeHTTP(0xc0000ec000, 0x881680, 0xc000130b60, 0xc000118b00)
/snap/go/4901/src/net/http/server.go:2802 +0xa4
net/http.(*conn).serve(0xc0000b1ea0, 0x881f40, 0xc00011aa00)
/snap/go/4901/src/net/http/server.go:1890 +0x875
created by net/http.(*Server).Serve
/snap/go/4901/src/net/http/server.go:2928 +0x384
method: GET
2020/01/02 17:31:25 http: panic serving 127.0.0.1:34912: runtime error: invalid memory address or nil pointer dereference
goroutine 99 [running]:
net/http.(*conn).serve.func1(0xc0003680a0)
/snap/go/4901/src/net/http/server.go:1767 +0x139
panic(0x78de40, 0xae4fe0)
/snap/go/4901/src/runtime/panic.go:679 +0x1b2
html/template.(*Template).escape(0x0, 0x0, 0x0)
/snap/go/4901/src/html/template/template.go:95 +0x42
html/template.(*Template).Execute(0x0, 0x87b880, 0xc0000ecc40, 0x0, 0x0, 0x87b9c0, 0xc00026c750)
/snap/go/4901/src/html/template/template.go:119 +0x2f
main.login(0x881680, 0xc0000ecc40, 0xc000118c00)
/home/mmt8037/go/src/awesomeProject1/main/goWeb.go:29 +0x357
net/http.HandlerFunc.ServeHTTP(0x815428, 0x881680, 0xc0000ecc40, 0xc000118c00)
/snap/go/4901/src/net/http/server.go:2007 +0x44
net/http.(*ServeMux).ServeHTTP(0xaf4e40, 0x881680, 0xc0000ecc40, 0xc000118c00)
/snap/go/4901/src/net/http/server.go:2387 +0x1bd
net/http.serverHandler.ServeHTTP(0xc0000ec000, 0x881680, 0xc0000ecc40, 0xc000118c00)
/snap/go/4901/src/net/http/server.go:2802 +0xa4
net/http.(*conn).serve(0xc0003680a0, 0x881f40, 0xc00011aa80)
/snap/go/4901/src/net/http/server.go:1890 +0x875
created by net/http.(*Server).Serve
/snap/go/4901/src/net/http/server.go:2928 +0x384
method: GET
2020/01/02 17:31:25 http: panic serving 127.0.0.1:34914: runtime error: invalid memory address or nil pointer dereference
goroutine 70 [running]:
net/http.(*conn).serve.func1(0xc000120820)
/snap/go/4901/src/net/http/server.go:1767 +0x139
panic(0x78de40, 0xae4fe0)
/snap/go/4901/src/runtime/panic.go:679 +0x1b2
html/template.(*Template).escape(0x0, 0x0, 0x0)
/snap/go/4901/src/html/template/template.go:95 +0x42
html/template.(*Template).Execute(0x0, 0x87b880, 0xc0000ecd20, 0x0, 0x0, 0x87b9c0, 0xc00026c870)
/snap/go/4901/src/html/template/template.go:119 +0x2f
main.login(0x881680, 0xc0000ecd20, 0xc000115000)
/home/mmt8037/go/src/awesomeProject1/main/goWeb.go:29 +0x357
net/http.HandlerFunc.ServeHTTP(0x815428, 0x881680, 0xc0000ecd20, 0xc000115000)
/snap/go/4901/src/net/http/server.go:2007 +0x44
net/http.(*ServeMux).ServeHTTP(0xaf4e40, 0x881680, 0xc0000ecd20, 0xc000115000)
/snap/go/4901/src/net/http/server.go:2387 +0x1bd
net/http.serverHandler.ServeHTTP(0xc0000ec000, 0x881680, 0xc0000ecd20, 0xc000115000)
/snap/go/4901/src/net/http/server.go:2802 +0xa4
net/http.(*conn).serve(0xc000120820, 0x881f40, 0xc0000b31c0)
/snap/go/4901/src/net/http/server.go:1890 +0x875
created by net/http.(*Server).Serve
/snap/go/4901/src/net/http/server.go:2928 +0x384
Please help me to find what’s wrong in my code.
Thanks in advance.