关于golang HandlerFunc。我以为404找不到,但是

关于golang HandlerFunc。我以为404找不到,但是,go,Go,这是我的密码 package main import "encoding/json" import "net/http" import "time" import "fmt" import "os" type Profile struct { Name string Hobbies []string } func main() { http.HandleFunc("/", rootFunc)//routeSet() err :=http.

这是我的密码

  package main

  import "encoding/json"
  import "net/http"
  import "time"
  import "fmt"
  import "os"

type Profile struct {
    Name string
    Hobbies []string
}

func main() {
   http.HandleFunc("/", rootFunc)//routeSet()
   err :=http.ListenAndServe(":3000", nil)
   checkError(err)
}

func checkError(err error) {
    if err != nil {
        fmt.Println("Fatal Error", err.Error())
        os.Exit(1)
    }
}

func rootFunc(w http.ResponseWriter, r *http.Request) {
    //fmt.Println("Request url : " + r.RequestURI)
    userProfile := make(chan Profile)

    go goFun(userProfile, w, r)

    profile := <-userProfile
    js, err := json.Marshal(profile)

    if err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
        return
    }

    w.Header().Set("Content-Type", "application/json")
    w.Write(js)
}
func goFun(u chan Profile, w http.ResponseWriter, r *http.Request) {
//    time.Sleep(1 * time.Second)
    u <- Profile{"Alex", []string{r.RequestURI, "programming"}}
}
我希望找不到404,因为我只对“/”使用HandleFunc()

但我得到了正常的结果

。。。。。。。。。。。。。。。。。。。。。。。。。。。 环境。 加油1.6 最大osx


是一个根子树,基本上匹配所有内容。看见(在做出任何假设之前,请务必查阅文档。)

“/”
是一个根子树,基本上匹配所有内容。看见(在做出任何假设之前,请务必查阅文档。)

您可以做的是添加
r.URL
检查,然后使用
http.Error
返回404,您可以做的是添加
r.URL
检查,然后使用
http.Error
返回404
{
    "Name": "Alex",
    "Hobbies": [
        "/hello",
        "programming"
    ]
}