错误:获取页面请求

huangapple go评论118阅读模式
英文:

Error GET page request

问题

我是你的中文翻译助手,以下是代码的翻译:

我刚开始学习Golang,尝试创建一个简单的Web服务器,但在GET请求中遇到了错误。以下是代码:

  1. import (
  2. "fmt"
  3. "html/template"
  4. "log"
  5. "net/http"
  6. "strings"
  7. )
  8. func sayhelloName(w http.ResponseWriter, r *http.Request) {
  9. r.ParseForm()
  10. fmt.Println(r.Form) // 在服务器端打印信息
  11. fmt.Println("path", r.URL.Path)
  12. fmt.Println("scheme", r.URL.Scheme)
  13. fmt.Println(r.Form["url_long"])
  14. for k, v := range r.Form {
  15. fmt.Println("key:", k)
  16. fmt.Println("val:", strings.Join(v, ""))
  17. }
  18. fmt.Fprintf(w, "Hello World!") // 写入响应数据
  19. }
  20. func login(w http.ResponseWriter, r *http.Request) {
  21. fmt.Println("method:", r.Method) // 获取请求方法
  22. if r.Method == "GET" {
  23. t, _ := template.ParseFiles("login.gtpl")
  24. t.Execute(w, nil)
  25. } else {
  26. r.ParseForm()
  27. // 登录逻辑部分
  28. fmt.Println("username:", r.Form["username"])
  29. fmt.Println("password:", r.Form["password"])
  30. }
  31. }
  32. func main() {
  33. http.HandleFunc("/", sayhelloName) // 设置路由规则
  34. http.HandleFunc("/login", login)
  35. err := http.ListenAndServe(":9090", nil) // 设置监听端口
  36. if err != nil {
  37. log.Fatal("ListenAndServe: ", err)
  38. }
  39. }

sayhelloName 函数可以正常工作,但 login 函数出现了问题。以下是错误输出:

  1. method: GET
  2. 2015/10/18 12:46:28 http: panic serving 127.0.0.1:51714: runtime error: invalid memory address or nil pointer dereference
  3. goroutine 6 [running]:
  4. net/http.(*conn).serve.func1(0x1247aa20, 0xaf5380, 0x12490390)
  5. c:/go/src/net/http/server.go:1287 +0xa2
  6. html/template.(*Template).escape(0x0, 0x0, 0x0)
  7. c:/go/src/html/template/template.go:79 +0x37
  8. html/template.(*Template).Execute(0x0, 0xaf54e8, 0x12530080, 0x0, 0x0, 0x0, 0x0)
  9. c:/go/src/html/template/template.go:101 +0x2b
  10. main.login(0xaf5468, 0x12530080, 0x124f6620)
  11. C:/Users/Fabio/Go/hello.go:30 +0x209
  12. net/http.HandlerFunc.ServeHTTP(0x7462e4, 0xaf5468, 0x12530080, 0x124f6620)
  13. c:/go/src/net/http/server.go:1422 +0x34
  14. net/http.(*ServeMux).ServeHTTP(0x1252a020, 0xaf5468, 0x12530080, 0x124f6620)
  15. c:/go/src/net/http/server.go:1699 +0x133
  16. net/http.serverHandler.ServeHTTP(0x12476480, 0xaf5468, 0x12530080, 0x124f6620)
  17. c:/go/src/net/http/server.go:1862 +0x156
  18. net/http.(*conn).serve(0x1247aa20)
  19. c:/go/src/net/http/server.go:1361 +0xc05
  20. created by net/http.(*Server).Serve
  21. c:/go/src/net/http/server.go:1910 +0x343
  22. login.gptl 文件与源代码文件位于同一文件夹中。
  23. <details>
  24. <summary>英文:</summary>
  25. I&#39;m new to Golang and I&#39;m trying to create a simple web server but I&#39;m getting an error in a GET request. This is the code:
  26. import (
  27. &quot;fmt&quot;
  28. &quot;html/template&quot;
  29. &quot;log&quot;
  30. &quot;net/http&quot;
  31. &quot;strings&quot;
  32. )
  33. func sayhelloName(w http.ResponseWriter, r *http.Request) {
  34. r.ParseForm()
  35. fmt.Println(r.Form) // print information on server side.
  36. fmt.Println(&quot;path&quot;, r.URL.Path)
  37. fmt.Println(&quot;scheme&quot;, r.URL.Scheme)
  38. fmt.Println(r.Form[&quot;url_long&quot;])
  39. for k, v := range r.Form {
  40. fmt.Println(&quot;key:&quot;, k)
  41. fmt.Println(&quot;val:&quot;, strings.Join(v, &quot;&quot;))
  42. }
  43. fmt.Fprintf(w, &quot;Hello Worrld!&quot;) // write data to response
  44. }
  45. func login(w http.ResponseWriter, r *http.Request) {
  46. fmt.Println(&quot;method:&quot;, r.Method) //get request method
  47. if r.Method == &quot;GET&quot; {
  48. t, _ := template.ParseFiles(&quot;login.gtpl&quot;)
  49. t.Execute(w, nil)
  50. } else {
  51. r.ParseForm()
  52. // logic part of log in
  53. fmt.Println(&quot;username:&quot;, r.Form[&quot;username&quot;])
  54. fmt.Println(&quot;password:&quot;, r.Form[&quot;password&quot;])
  55. }
  56. }
  57. func main() {
  58. http.HandleFunc(&quot;/&quot;, sayhelloName) // setting router rule
  59. http.HandleFunc(&quot;/login&quot;, login)
  60. err := http.ListenAndServe(&quot;:9090&quot;, nil) // setting listening port
  61. if err != nil {
  62. log.Fatal(&quot;ListenAndServe: &quot;, err)
  63. }
  64. }
  65. `sayhelloName` works but not the `login` function. This is the output error:
  66. method: GET
  67. 2015/10/18 12:46:28 http: panic serving 127.0.0.1:51714: runtime error: invalid memory address or nil pointer dereference
  68. goroutine 6 [running]:
  69. net/http.(*conn).serve.func1(0x1247aa20, 0xaf5380, 0x12490390)
  70. c:/go/src/net/http/server.go:1287 +0xa2
  71. html/template.(*Template).escape(0x0, 0x0, 0x0)
  72. c:/go/src/html/template/template.go:79 +0x37
  73. html/template.(*Template).Execute(0x0, 0xaf54e8, 0x12530080, 0x0, 0x0, 0x0, 0x0)
  74. c:/go/src/html/template/template.go:101 +0x2b
  75. main.login(0xaf5468, 0x12530080, 0x124f6620)
  76. C:/Users/Fabio/Go/hello.go:30 +0x209
  77. net/http.HandlerFunc.ServeHTTP(0x7462e4, 0xaf5468, 0x12530080, 0x124f6620)
  78. c:/go/src/net/http/server.go:1422 +0x34
  79. net/http.(*ServeMux).ServeHTTP(0x1252a020, 0xaf5468, 0x12530080, 0x124f6620)
  80. c:/go/src/net/http/server.go:1699 +0x133
  81. net/http.serverHandler.ServeHTTP(0x12476480, 0xaf5468, 0x12530080, 0x124f6620)
  82. c:/go/src/net/http/server.go:1862 +0x156
  83. net/http.(*conn).serve(0x1247aa20)
  84. c:/go/src/net/http/server.go:1361 +0xc05
  85. created by net/http.(*Server).Serve
  86. c:/go/src/net/http/server.go:1910 +0x343
  87. login.gptl it&#39;s in the same folder of source code file
  88. </details>
  89. # 答案1
  90. **得分**: 3
  91. 始终处理错误
  92. t_:= template.ParseFiles(“login.gtpl”)
  93. 如果这失败了怎么办?那么`t`将为nil,你将得到相同的错误。
  94. <details>
  95. <summary>英文:</summary>
  96. Always handle your errors
  97. t, _ := template.ParseFiles(&quot;login.gtpl&quot;)
  98. What if this fails? Then `t` is nil and you get the same error
  99. </details>
  100. # 答案2
  101. **得分**: 0
  102. 1. 不要忽略从template.ParseFiles返回的错误。
  103. 2. 将包括目录在内的路径放入template.ParseFiles中。
  104. <details>
  105. <summary>英文:</summary>
  106. 1. don&#39;t ignore error returned from template.ParseFiles
  107. 2. put path including directory into template.ParseFiles
  108. </details>

huangapple
  • 本文由 发表于 2015年10月18日 18:42:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/33196989.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定