关于golang的HandlerFunc。我期望得到404未找到的结果,但是…

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

about golang HandlerFunc. I expected 404 not found but

问题

这是我的代码:

  1. package main
  2. import "encoding/json"
  3. import "net/http"
  4. import "time"
  5. import "fmt"
  6. import "os"
  7. type Profile struct {
  8. Name string
  9. Hobbies []string
  10. }
  11. func main() {
  12. http.HandleFunc("/", rootFunc) //routeSet()
  13. err := http.ListenAndServe(":3000", nil)
  14. checkError(err)
  15. }
  16. func checkError(err error) {
  17. if err != nil {
  18. fmt.Println("Fatal Error", err.Error())
  19. os.Exit(1)
  20. }
  21. }
  22. func rootFunc(w http.ResponseWriter, r *http.Request) {
  23. //fmt.Println("Request url : " + r.RequestURI)
  24. userProfile := make(chan Profile)
  25. go goFun(userProfile, w, r)
  26. profile := <-userProfile
  27. js, err := json.Marshal(profile)
  28. if err != nil {
  29. http.Error(w, err.Error(), http.StatusInternalServerError)
  30. return
  31. }
  32. w.Header().Set("Content-Type", "application/json")
  33. w.Write(js)
  34. }
  35. func goFun(u chan Profile, w http.ResponseWriter, r *http.Request) {
  36. // time.Sleep(1 * time.Second)
  37. u <- Profile{"Alex", []string{r.RequestURI, "programming"}}
  38. }

我通过Postman发送了http://localhost:3000/hello,并收到了以下响应:

  1. {
  2. "Name": "Alex",
  3. "Hobbies": [
  4. "/hello",
  5. "programming"
  6. ]
  7. }

我预期会收到404 Not Found,因为我只为"/"使用了HandleFunc(),但我收到了正常的结果。

环境:
Go 1.6
Mac OS X

英文:

this is my code

  1. package main
  2. import &quot;encoding/json&quot;
  3. import &quot;net/http&quot;
  4. import &quot;time&quot;
  5. import &quot;fmt&quot;
  6. import &quot;os&quot;
  7. type Profile struct {
  8. Name string
  9. Hobbies []string
  10. }
  11. func main() {
  12. http.HandleFunc(&quot;/&quot;, rootFunc)//routeSet()
  13. err :=http.ListenAndServe(&quot;:3000&quot;, nil)
  14. checkError(err)
  15. }
  16. func checkError(err error) {
  17. if err != nil {
  18. fmt.Println(&quot;Fatal Error&quot;, err.Error())
  19. os.Exit(1)
  20. }
  21. }
  22. func rootFunc(w http.ResponseWriter, r *http.Request) {
  23. //fmt.Println(&quot;Request url : &quot; + r.RequestURI)
  24. userProfile := make(chan Profile)
  25. go goFun(userProfile, w, r)
  26. profile := &lt;-userProfile
  27. js, err := json.Marshal(profile)
  28. if err != nil {
  29. http.Error(w, err.Error(), http.StatusInternalServerError)
  30. return
  31. }
  32. w.Header().Set(&quot;Content-Type&quot;, &quot;application/json&quot;)
  33. w.Write(js)
  34. }
  35. func goFun(u chan Profile, w http.ResponseWriter, r *http.Request) {
  36. // time.Sleep(1 * time.Second)
  37. u &lt;- Profile{&quot;Alex&quot;, []string{r.RequestURI, &quot;programming&quot;}}
  38. }

and I send http://localhost:3000/hello by postman

and I recieve

  1. {
  2. &quot;Name&quot;: &quot;Alex&quot;,
  3. &quot;Hobbies&quot;: [
  4. &quot;/hello&quot;,
  5. &quot;programming&quot;
  6. ]
  7. }

I expected 404 not found, because I use HandleFunc() only for "/"

but i received normal result.

...........................
env.
go 1.6
max osx

...........................

答案1

得分: 3

"/" 是一个根节点的子树,基本上可以匹配任何内容。请参考 https://golang.org/pkg/net/http/#ServeMux (在做任何假设之前,请始终查阅文档)。

英文:

&quot;/&quot; is a rooted subtree and matches basically everything. See https://golang.org/pkg/net/http/#ServeMux . (Always consult the documentation before making any assumptions.)

huangapple
  • 本文由 发表于 2016年3月14日 16:33:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/35982751.html
匿名

发表评论

匿名网友

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

确定