如何使GoLang的http.HandleFunc()函数正常工作而不出错?

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

How do I get GoLang's http.HandleFunc() to work without error?

问题

我正在尝试创建一个GoLang的http.HandleFunc()函数,用于监听POST请求并解析JSON文件。但是,这个处理程序在启动时出现错误并返回一个错误信息:

> 2017/01/24 13:35:08 listen tcp :8080: bind: Only one usage of each
> socket address (protocol/network address/port) is normally permitted.

我猜测这是一个致命错误。

以下是我的函数代码:

  1. http.HandleFunc("/", func(w http.ResponseWriter, request *http.Request) {
  2. //debug
  3. fmt.Fprintf(w, "Hello, %q", html.EscapeString(request.URL.Path))
  4. var m Message
  5. if request.Body == nil {
  6. http.Error(w, "Please send a request body", 400)
  7. return
  8. }
  9. err := json.NewDecoder(request.Body).Decode(&m)
  10. if err != nil {
  11. http.Error(w, err.Error(), 400)
  12. return
  13. }
  14. })
  15. log.Fatal(http.ListenAndServe(":8080", nil))

请问如何使这个处理程序正常工作,避免出现错误?

英文:

I am trying to create a GoLang http.HandleFunc() that will listen for post requests and parse a json file. The Handler seems to fail as it starts and returns an error:

> 2017/01/24 13:35:08 listen tcp :8080: bind: Only one usage of each
> socket address (protocol/network address/port) is normally permitted.

I am assuming that it is throwing a fatal error.

Below is my function:

  1. http.HandleFunc("/", func(w http.ResponseWriter, request *http.Request) {
  2. //debug
  3. fmt.Fprintf(w, "Hello, %q", html.EscapeString(request.URL.Path))
  4. var m Message
  5. if request.Body == nil {
  6. http.Error(w, "Please send a request body", 400)
  7. return
  8. }
  9. err := json.NewDecoder(request.Body).Decode(&m)
  10. if err != nil {
  11. http.Error(w, err.Error(), 400)
  12. return
  13. }
  14. })
  15. log.Fatal(http.ListenAndServe(":8080", nil))

How can I get this Handler to work and not get the error?

答案1

得分: 1

我已经修复了你的代码中的一些问题,并根据缺失的信息做出了最佳猜测。以下是一个示例工作代码,它将接受一个具有"name"和"text"字段的消息:

  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "html"
  6. "log"
  7. "net/http"
  8. )
  9. type Message struct {
  10. Name string `json:"name"`
  11. Text string `json:"text"`
  12. }
  13. func main() {
  14. http.HandleFunc("/", func(w http.ResponseWriter, request *http.Request) {
  15. fmt.Fprintf(w, "Hello, %q", html.EscapeString(request.URL.Path))
  16. var m Message
  17. if request.Body == nil {
  18. http.Error(w, "Please send a request body", 400)
  19. return
  20. }
  21. err := json.NewDecoder(request.Body).Decode(&m)
  22. if err != nil {
  23. http.Error(w, err.Error(), 400)
  24. return
  25. }
  26. log.Println(m)
  27. })
  28. log.Fatal(http.ListenAndServe(":8080", nil))
  29. }

希望对你有帮助!

英文:

I've remedied several of the problems with your code and made best guesses around missing information. Here is sample working code that will accept a message that has "name" and "text" fields:

  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "html"
  6. "log"
  7. "net/http"
  8. )
  9. type Message struct {
  10. Name string `json:"name"`
  11. Text string `json:"text"`
  12. }
  13. func main() {
  14. http.HandleFunc("/", func(w http.ResponseWriter, request *http.Request) {
  15. fmt.Fprintf(w, "Hello, %q", html.EscapeString(request.URL.Path))
  16. var m Message
  17. if request.Body == nil {
  18. http.Error(w, "Please send a request body", 400)
  19. return
  20. }
  21. err := json.NewDecoder(request.Body).Decode(&m)
  22. if err != nil {
  23. http.Error(w, err.Error(), 400)
  24. return
  25. }
  26. log.Println(m)
  27. })
  28. log.Fatal(http.ListenAndServe(":8080", nil))
  29. }

huangapple
  • 本文由 发表于 2017年1月25日 02:46:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/41836655.html
匿名

发表评论

匿名网友

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

确定