duanlu1876 2015-10-23 11:07
浏览 65
已采纳

去http紧急服务

I got this panic:

C:\Users\loow\Desktop\USBWebserver v8.5\duplicate_submissions>go run server.go
2015/10/23 13:00:39 http: panic serving [::1]:63867: runtime error: invalid memo
ry address or nil pointer dereference
goroutine 5 [running]:
net/http.(*conn).serve.func1(0xc0820a1810, 0x3b55b8, 0xc082024040)
        c:/go/src/net/http/server.go:1287 +0xbc
main.login(0x2990058, 0xc0820d4000, 0xc0820be1c0)
        C:/Users/loow/Desktop/USBWebserver v8.5/duplicate_submissions/server.go:
27 +0x5a5
net/http.HandlerFunc.ServeHTTP(0x8326a8, 0x2990058, 0xc0820d4000, 0xc0820be1c0)
        c:/go/src/net/http/server.go:1422 +0x41
net/http.(*ServeMux).ServeHTTP(0xc082062360, 0x2990058, 0xc0820d4000, 0xc0820be1
c0)
        c:/go/src/net/http/server.go:1699 +0x184
net/http.serverHandler.ServeHTTP(0xc08200c360, 0x2990058, 0xc0820d4000, 0xc0820b
e1c0)
        c:/go/src/net/http/server.go:1862 +0x1a5
net/http.(*conn).serve(0xc0820a1810)
        c:/go/src/net/http/server.go:1361 +0xbf5
created by net/http.(*Server).Serve
        c:/go/src/net/http/server.go:1910 +0x3fd
2015/10/23 13:00:39 http: panic serving [::1]:63868: runtime error: invalid memo
ry address or nil pointer dereference
goroutine 33 [running]:
net/http.(*conn).serve.func1(0xc082114000, 0x3b55b8, 0xc082112000)
        c:/go/src/net/http/server.go:1287 +0xbc
main.login(0x2990058, 0xc0821140b0, 0xc0821200e0)
        C:/Users/loow/Desktop/USBWebserver v8.5/duplicate_submissions/server.go:
27 +0x5a5
net/http.HandlerFunc.ServeHTTP(0x8326a8, 0x2990058, 0xc0821140b0, 0xc0821200e0)
        c:/go/src/net/http/server.go:1422 +0x41
net/http.(*ServeMux).ServeHTTP(0xc082062360, 0x2990058, 0xc0821140b0, 0xc0821200
e0)
        c:/go/src/net/http/server.go:1699 +0x184
net/http.serverHandler.ServeHTTP(0xc08200c360, 0x2990058, 0xc0821140b0, 0xc08212
00e0)
        c:/go/src/net/http/server.go:1862 +0x1a5
net/http.(*conn).serve(0xc082114000)
        c:/go/src/net/http/server.go:1361 +0xbf5
created by net/http.(*Server).Serve
        c:/go/src/net/http/server.go:1910 +0x3fd
exit status 2

Whit this code:

package main

import(
    "fmt"
    "net/http"
    "html/template"
    "log"
    "time"
    "crypto/md5"
    "io"
    "strconv"
)

func loginForm(w http.ResponseWriter, r *http.Request){

}

func login(w http.ResponseWriter, r *http.Request){
    fmt.Println(r.Method)
    if r.Method == "GET"{
        cruTime := time.Now().Unix()
        h := md5.New()
        io.WriteString(h,strconv.FormatInt(cruTime,10))
        token := fmt.Sprintf("%x", h.Sum(nil))
        fmt.Println(token)
        t, err := template.ParseFiles("templates/index.gtpl")
        fmt.Println(err.Error())
        err = t.Execute(w, token)
        fmt.Println(err.Error())
    } else{
        r.ParseForm()
        token := r.Form.Get("token")
        if token != ""{
            fmt.Println(token)
        } else{
            fmt.Println("There is no token")
        }
        fmt.Println("username length: ", len(r.Form["username"][0]))
        fmt.Println("username: ", template.HTMLEscapeString(r.Form.Get("username")))
        fmt.Println("password: ", template.HTMLEscapeString(r.Form.Get("password")))
        template.HTMLEscape(w, []byte(r.Form.Get("username")))

    }
}

func main(){
    http.HandleFunc("/", loginForm)
    http.HandleFunc("/login", login)
    err := http.ListenAndServe(":9090", nil)
    if err != nil {
        log.Fatal("ListenAndServe: ", err)
    }
}

I cant fix it, I tried everything what I found in the stackoverflow. What is the problem? There is no error, and the panic said that the problem in t, err := template.ParseFiles("templates/index.gtpl")..

There is the template file:

<input type="checkbox" name="interest" value="football">Football
<input type="checkbox" name="interest" value="basketball">Basketball
<input type="checkbox" name="interest" value="tennis">Tennis
Username: <input type="text" name="username">
Password: <input type="password" name="password">
<input type="hidden" name="token" value="{{.}}">
<input type="submit" value="Login">
  • 写回答

1条回答 默认 最新

  • dsfbnhc4373 2015-10-23 15:10
    关注

    The panic stacktrace gives you this info :

    2015/10/23 13:00:39 http: panic serving [::1]:63868: runtime error: invalid memory address or nil pointer dereference goroutine 33 [running]:
    

    It means you're trying to access something that does not exist (nil pointer).

    Then the first line that comes from your file is this one :

    v8.5/duplicate_submissions/server.go:27
    

    Which is there :

    26: t, err := template.ParseFiles("templates/index.gtpl")
    27: fmt.Println(err.Error())
    28: err = t.Execute(w, token)
    

    It means err is nil.

    Solution

    If you get the error, you cannot continue the process. That's the reason why you cannot just print out the error. In order to stop gracefully the process, you need to return an HTTP status code and then return. For the case above, you can return a code 500 (internal server error).

    t, err := template.ParseFiles("templates/index.gtpl")
    if err != nil {
      fmt.Println(err) // Ugly debug output
      w.WriteHeader(http.StatusInternalServerError) // Proper HTTP response
      return
    }
    

    That has to be done for a template.ParseFiles and t.Execute too.

    By the way, that is called the "comma ok" pattern

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)