astaxie / build-web-application-with-golang

A golang ebook intro how to build a web with golang
BSD 3-Clause "New" or "Revised" License
43.23k stars 10.65k forks source link

中文版4.1处理表单的输入示例执行报错? #821

Open djstava opened 7 years ago

djstava commented 7 years ago

执行环境: Windows 10、Go1.8

错误信息如下: method: GET 2017/04/11 14:46:22 http: panic serving 127.0.0.1:56224: runtime error: invalid memory address or nil pointer dereference goroutine 5 [running]: net/http.(conn).serve.func1(0xc042038be0) D:/tools/go/src/net/http/server.go:1721 +0xd7 panic(0x717700, 0x902950) D:/tools/go/src/runtime/panic.go:489 +0x2dd html/template.(Template).escape(0x0, 0x0, 0x0) D:/tools/go/src/html/template/template.go:94 +0x3f html/template.(Template).Execute(0x0, 0x8db500, 0xc0420e81c0, 0x0, 0x0, 0xc0420dc1e0, 0xc04202bcc0) D:/tools/go/src/html/template/template.go:117 +0x36 main.login(0x8df180, 0xc0420e81c0, 0xc042032100) D:/workshop/go/src/webGo.go:30 +0x476 net/http.HandlerFunc.ServeHTTP(0x786be0, 0x8df180, 0xc0420e81c0, 0xc042032100) D:/tools/go/src/net/http/server.go:1942 +0x4b net/http.(ServeMux).ServeHTTP(0x90f480, 0x8df180, 0xc0420e81c0, 0xc042032100) D:/tools/go/src/net/http/server.go:2238 +0x137 net/http.serverHandler.ServeHTTP(0xc04206ec60, 0x8df180, 0xc0420e81c0, 0xc042032100) D:/tools/go/src/net/http/server.go:2568 +0x99 net/http.(conn).serve(0xc042038be0, 0x8df740, 0xc0420d8040) D:/tools/go/src/net/http/server.go:1825 +0x619 created by net/http.(Server).Serve D:/tools/go/src/net/http/server.go:2668 +0x2d5

yb3616 commented 7 years ago

你GET上去的数据,执行到解析html模板那里出错了,因为程序找不到该模板文件。 解决方案:在同级目录下新建一个名为“login.gtpl”的文件,并随意写入内容就行。

另外,如果你POST上去的数据,就会发现程序无法解析变量,因为在“login”函数中的"else"语句中缺少解析form方法,加上"r.ParseForm()"即可

ResolveWang commented 7 years ago

确实是文件路径问题,我也遇到了和你@djstava 一样的问题,我把模版文件放根目录,结果就不报错了

SolarianZ commented 7 years ago

解决办法:login.gtpl文件放到src目录下,最后的项目结构应该是这样的:

$GOPATH/ ----|----bin/ ----|----pkg/ ----|----src/ ----|----|----main/ ----|----|----|----main.go ----|----|----login.gtpl

iafine commented 7 years ago

除了上面的解决办法,也可以修改下代码路径,类似如下:

t, err := template.ParseFiles("src/main/login.gtpl")

zjutjsj1004 commented 5 years ago

我在google浏览器中输入:http://127.0.0.1:9090/login 还是出现了异常情况: method: GET 2018/11/19 22:53:19 http: panic serving 127.0.0.1:40186: runtime error: invalid memory address or nil pointer dereference goroutine 5 [running]: net/http.(conn).serve.func1(0xc000046e60) C:/Go/src/net/http/server.go:1746 +0xd7 panic(0x727240, 0xa10b30) C:/Go/src/runtime/panic.go:513 +0x1c7 html/template.(Template).escape(0x0, 0x0, 0x0) C:/Go/src/html/template/template.go:95 +0x35 html/template.(Template).Execute(0x0, 0x7df1a0, 0xc0000ee000, 0x0, 0x0, 0xc0000dc0c0, 0x6) C:/Go/src/html/template/template.go:119 +0x36 main.login(0x7e1b60, 0xc0000ee000, 0xc0000d4000) F:/github/dailyapp.git/go/src/github.com/go-web-program/form/login.go:30 +0x34e net/http.HandlerFunc.ServeHTTP(0x79df18, 0x7e1b60, 0xc0000ee000, 0xc0000d4000) C:/Go/src/net/http/server.go:1964 +0x4b net/http.(ServeMux).ServeHTTP(0xa1f840, 0x7e1b60, 0xc0000ee000, 0xc0000d4000) C:/Go/src/net/http/server.go:2361 +0x12e net/http.serverHandler.ServeHTTP(0xc00004ad00, 0x7e1b60, 0xc0000ee000, 0xc0000d4000) C:/Go/src/net/http/server.go:2741 +0xb2 net/http.(conn).serve(0xc000046e60, 0x7e1d20, 0xc00003a280) C:/Go/src/net/http/server.go:1847 +0x64d created by net/http.(Server).Serve C:/Go/src/net/http/server.go:2851 +0x2fc method: GET 2018/11/19 22:53:19 http: panic serving 127.0.0.1:40187: runtime error: invalid memory address or nil pointer dereference goroutine 6 [running]: net/http.(conn).serve.func1(0xc000046f00) C:/Go/src/net/http/server.go:1746 +0xd7 panic(0x727240, 0xa10b30) C:/Go/src/runtime/panic.go:513 +0x1c7 html/template.(Template).escape(0x0, 0x0, 0x0) C:/Go/src/html/template/template.go:95 +0x35 html/template.(Template).Execute(0x0, 0x7df1a0, 0xc000138000, 0x0, 0x0, 0xc00005b290, 0x6) C:/Go/src/html/template/template.go:119 +0x36 main.login(0x7e1b60, 0xc000138000, 0xc0000d4100) F:/github/dailyapp.git/go/src/github.com/go-web-program/form/login.go:30 +0x34e net/http.HandlerFunc.ServeHTTP(0x79df18, 0x7e1b60, 0xc000138000, 0xc0000d4100) C:/Go/src/net/http/server.go:1964 +0x4b net/http.(ServeMux).ServeHTTP(0xa1f840, 0x7e1b60, 0xc000138000, 0xc0000d4100) C:/Go/src/net/http/server.go:2361 +0x12e net/http.serverHandler.ServeHTTP(0xc00004ad00, 0x7e1b60, 0xc000138000, 0xc0000d4100) C:/Go/src/net/http/server.go:2741 +0xb2 net/http.(conn).serve(0xc000046f00, 0x7e1d20, 0xc00003a340) C:/Go/src/net/http/server.go:1847 +0x64d created by net/http.(Server).Serve C:/Go/src/net/http/server.go:2851 +0x2fc method: GET 2018/11/19 22:53:19 http: panic serving 127.0.0.1:40188: runtime error: invalid memory address or nil pointer dereference goroutine 34 [running]: net/http.(conn).serve.func1(0xc000160000) C:/Go/src/net/http/server.go:1746 +0xd7 panic(0x727240, 0xa10b30) C:/Go/src/runtime/panic.go:513 +0x1c7 html/template.(Template).escape(0x0, 0x0, 0x0) C:/Go/src/html/template/template.go:95 +0x35 html/template.(Template).Execute(0x0, 0x7df1a0, 0xc0001380e0, 0x0, 0x0, 0xc00005b3b0, 0x6) C:/Go/src/html/template/template.go:119 +0x36 main.login(0x7e1b60, 0xc0001380e0, 0xc0000d4200) F:/github/dailyapp.git/go/src/github.com/go-web-program/form/login.go:30 +0x34e net/http.HandlerFunc.ServeHTTP(0x79df18, 0x7e1b60, 0xc0001380e0, 0xc0000d4200) C:/Go/src/net/http/server.go:1964 +0x4b net/http.(ServeMux).ServeHTTP(0xa1f840, 0x7e1b60, 0xc0001380e0, 0xc0000d4200) C:/Go/src/net/http/server.go:2361 +0x12e net/http.serverHandler.ServeHTTP(0xc00004ad00, 0x7e1b60, 0xc0001380e0, 0xc0000d4200) C:/Go/src/net/http/server.go:2741 +0xb2 net/http.(conn).serve(0xc000160000, 0x7e1d20, 0xc00003a580) C:/Go/src/net/http/server.go:1847 +0x64d created by net/http.(Server).Serve C:/Go/src/net/http/server.go:2851 +0x2fc

zjutjsj1004 commented 5 years ago

除了上面的解决办法,也可以修改下代码路径,类似如下:

t, err := template.ParseFiles("src/main/login.gtpl")

我这样子修改了,还是没有用

zjutjsj1004 commented 5 years ago

解决办法:login.gtpl文件放到src目录下,最后的项目结构应该是这样的:

$GOPATH/ ----|----bin/ ----|----pkg/ ----|----src/ ----|----|----main/ ----|----|----|----main.go ----|----|----login.gtpl

我在src中添加了:login.gtpl 但是在google浏览器中输入:http://127.0.0.1:9090/login 还是出现了异常情况

zjutjsj1004 commented 5 years ago

我在google浏览器中输入:http://127.0.0.1:9090/login 还是出现了异常情况: method: GET 2018/11/19 22:53:19 http: panic serving 127.0.0.1:40186: runtime error: invalid memory address or nil pointer dereference goroutine 5 [running]: net/http.(conn).serve.func1(0xc000046e60) C:/Go/src/net/http/server.go:1746 +0xd7 panic(0x727240, 0xa10b30) C:/Go/src/runtime/panic.go:513 +0x1c7 html/template.(Template).escape(0x0, 0x0, 0x0) C:/Go/src/html/template/template.go:95 +0x35 html/template.(Template).Execute(0x0, 0x7df1a0, 0xc0000ee000, 0x0, 0x0, 0xc0000dc0c0, 0x6) C:/Go/src/html/template/template.go:119 +0x36 main.login(0x7e1b60, 0xc0000ee000, 0xc0000d4000) F:/github/dailyapp.git/go/src/github.com/go-web-program/form/login.go:30 +0x34e net/http.HandlerFunc.ServeHTTP(0x79df18, 0x7e1b60, 0xc0000ee000, 0xc0000d4000) C:/Go/src/net/http/server.go:1964 +0x4b net/http.(ServeMux).ServeHTTP(0xa1f840, 0x7e1b60, 0xc0000ee000, 0xc0000d4000) C:/Go/src/net/http/server.go:2361 +0x12e net/http.serverHandler.ServeHTTP(0xc00004ad00, 0x7e1b60, 0xc0000ee000, 0xc0000d4000) C:/Go/src/net/http/server.go:2741 +0xb2 net/http.(conn).serve(0xc000046e60, 0x7e1d20, 0xc00003a280) C:/Go/src/net/http/server.go:1847 +0x64d created by net/http.(Server).Serve C:/Go/src/net/http/server.go:2851 +0x2fc method: GET 2018/11/19 22:53:19 http: panic serving 127.0.0.1:40187: runtime error: invalid memory address or nil pointer dereference goroutine 6 [running]: net/http.(conn).serve.func1(0xc000046f00) C:/Go/src/net/http/server.go:1746 +0xd7 panic(0x727240, 0xa10b30) C:/Go/src/runtime/panic.go:513 +0x1c7 html/template.(Template).escape(0x0, 0x0, 0x0) C:/Go/src/html/template/template.go:95 +0x35 html/template.(Template).Execute(0x0, 0x7df1a0, 0xc000138000, 0x0, 0x0, 0xc00005b290, 0x6) C:/Go/src/html/template/template.go:119 +0x36 main.login(0x7e1b60, 0xc000138000, 0xc0000d4100) F:/github/dailyapp.git/go/src/github.com/go-web-program/form/login.go:30 +0x34e net/http.HandlerFunc.ServeHTTP(0x79df18, 0x7e1b60, 0xc000138000, 0xc0000d4100) C:/Go/src/net/http/server.go:1964 +0x4b net/http.(ServeMux).ServeHTTP(0xa1f840, 0x7e1b60, 0xc000138000, 0xc0000d4100) C:/Go/src/net/http/server.go:2361 +0x12e net/http.serverHandler.ServeHTTP(0xc00004ad00, 0x7e1b60, 0xc000138000, 0xc0000d4100) C:/Go/src/net/http/server.go:2741 +0xb2 net/http.(conn).serve(0xc000046f00, 0x7e1d20, 0xc00003a340) C:/Go/src/net/http/server.go:1847 +0x64d created by net/http.(Server).Serve C:/Go/src/net/http/server.go:2851 +0x2fc method: GET 2018/11/19 22:53:19 http: panic serving 127.0.0.1:40188: runtime error: invalid memory address or nil pointer dereference goroutine 34 [running]: net/http.(conn).serve.func1(0xc000160000) C:/Go/src/net/http/server.go:1746 +0xd7 panic(0x727240, 0xa10b30) C:/Go/src/runtime/panic.go:513 +0x1c7 html/template.(Template).escape(0x0, 0x0, 0x0) C:/Go/src/html/template/template.go:95 +0x35 html/template.(Template).Execute(0x0, 0x7df1a0, 0xc0001380e0, 0x0, 0x0, 0xc00005b3b0, 0x6) C:/Go/src/html/template/template.go:119 +0x36 main.login(0x7e1b60, 0xc0001380e0, 0xc0000d4200) F:/github/dailyapp.git/go/src/github.com/go-web-program/form/login.go:30 +0x34e net/http.HandlerFunc.ServeHTTP(0x79df18, 0x7e1b60, 0xc0001380e0, 0xc0000d4200) C:/Go/src/net/http/server.go:1964 +0x4b net/http.(ServeMux).ServeHTTP(0xa1f840, 0x7e1b60, 0xc0001380e0, 0xc0000d4200) C:/Go/src/net/http/server.go:2361 +0x12e net/http.serverHandler.ServeHTTP(0xc00004ad00, 0x7e1b60, 0xc0001380e0, 0xc0000d4200) C:/Go/src/net/http/server.go:2741 +0xb2 net/http.(conn).serve(0xc000160000, 0x7e1d20, 0xc00003a580) C:/Go/src/net/http/server.go:1847 +0x64d created by net/http.(Server).Serve C:/Go/src/net/http/server.go:2851 +0x2fc

解决了,因为我exe的运行路径 和 login.gtpl 有问题