go - "invalid memory address or nil pointer deference"做教程

标签 go

这是从教程中复制的确切代码。我是 Go 和 web 开发的新手,所以我很难调试此类错误。

 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)
    }
}

还有另一个名为 login.gtpl 的文件,包含基本的 html 代码

<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>

这是错误信息

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

最佳答案

您似乎正在执行无效或无模板。您忽略的错误可能是缺少的线索:

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

永远不要忽略错误。

相反,使用检查错误

t, err := template.ParseFiles("login.gtpl")
if err != nil {
    /* Report or handle the error here */
}

关于go - "invalid memory address or nil pointer deference"做教程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45264265/

相关文章:

go - Golang中没有固定长度的数组

go - 如何从多个 goroutines 共享的单个 channel 中读取

go - GORM 中的一对多递归关系

go - 我无法安装 go 包

Golang 调用/调用子项目的最佳方式

go - 发布一个 golang 模块 : Semantic versioning

go - 如何阻止 go html/template 转义路径(尝试过 .HTML .JS 等)

api - 未在Vue页面上检测到Cookie

http - 测试 http.Response.Body 是否关闭

go - Golang 应用程序的类似 PM2 的进程管理解决方案?