tophubs/TopList

runtime error: invalid memory address or nil pointer dereference

zyjia opened this issue · 4 comments

zyjia commented

环境:ubuntu 16.04 go语言:go version go1.13.1 linux/amd64
错误信息:
parse mysql config once. filePath: /home/tonyzhang/data/web/go/top/Config/mysql.toml
2019/10/08 13:03:40 err
2019/10/08 13:03:40 http: panic serving 192.168.99.198:5810: runtime error: invalid memory address or nil pointer dereference
goroutine 21 [running]:
net/http.(*conn).serve.func1(0xc0000bcc80)
/home/tonyzhang/data/go/src/net/http/server.go:1767 +0x139
panic(0x7fe6c0, 0xba3450)
/home/tonyzhang/data/go/src/runtime/panic.go:679 +0x1b2
text/template.errRecover(0xc000165b48)
/home/tonyzhang/data/go/src/text/template/exec.go:164 +0x1cb
panic(0x7fe6c0, 0xba3450)
/home/tonyzhang/data/go/src/runtime/panic.go:679 +0x1b2
text/template.(*Template).execute(0x0, 0x902920, 0xc000152000, 0x0, 0x0, 0x0, 0x0)
/home/tonyzhang/data/go/src/text/template/exec.go:218 +0x1c1
text/template.(*Template).Execute(...)
/home/tonyzhang/data/go/src/text/template/exec.go:204
main.main.func1(0x909960, 0xc000152000, 0xc000144000)
/home/tonyzhang/data/web/go/top/App/Server.go:72 +0xc7
net/http.HandlerFunc.ServeHTTP(0x88ff88, 0x909960, 0xc000152000, 0xc000144000)
/home/tonyzhang/data/go/src/net/http/server.go:2007 +0x44
net/http.(*ServeMux).ServeHTTP(0xbb09c0, 0x909960, 0xc000152000, 0xc000144000)
/home/tonyzhang/data/go/src/net/http/server.go:2387 +0x1bd
net/http.serverHandler.ServeHTTP(0xc000112000, 0x909960, 0xc000152000, 0xc000144000)
/home/tonyzhang/data/go/src/net/http/server.go:2802 +0xa4
net/http.(*conn).serve(0xc0000bcc80, 0x90a620, 0xc00013a000)
/home/tonyzhang/data/go/src/net/http/server.go:1890 +0x875
created by net/http.(*Server).Serve
/home/tonyzhang/data/go/src/net/http/server.go:2927 +0x38e

TopList/App/Server.go

Lines 63 to 73 in d7a1599

http.Handle("/css/", http.StripPrefix("/css/", http.FileServer(http.Dir("../Html/css/"))))
http.Handle("/js/", http.StripPrefix("/js/", http.FileServer(http.Dir("../Html/js/"))))
// 首页
http.HandleFunc("/", func(res http.ResponseWriter, req *http.Request) {
t, err := template.ParseFiles("../Html/hot.html")
if err != nil {
log.Println("err")
}
t.Execute(res, nil)
})

后面的相对路径换成绝对路径,window下测试可以使用

zyjia commented

TopList/App/Server.go

Lines 63 to 73 in d7a1599

http.Handle("/css/", http.StripPrefix("/css/", http.FileServer(http.Dir("../Html/css/"))))
http.Handle("/js/", http.StripPrefix("/js/", http.FileServer(http.Dir("../Html/js/"))))
// 首页
http.HandleFunc("/", func(res http.ResponseWriter, req *http.Request) {
t, err := template.ParseFiles("../Html/hot.html")
if err != nil {
log.Println("err")
}
t.Execute(res, nil)
})

后面的相对路径换成绝对路径,window下测试可以使用
谢谢,可以用了,就是有点别扭

感谢评论区

有效!