关于golang HandlerFunc。我预计404找不到但是

时间:2016-03-14 08:33:55

标签: 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.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"}}
}

我发送 http://localhost:3000/hello 邮递员

我收到了

{
    "Name": "Alex",
    "Hobbies": [
        "/hello",
        "programming"
    ]
}

我预计404找不到,因为我只将HandleFunc()用于&#34; /&#34;

但我收到了正常的结果。

........................... ENV。 去1.6 max osx

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

1 个答案:

答案 0 :(得分:3)

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { if(buttonIndex == 0)//Settings button pressed { if (alertView.tag == 100) { //This will open ios devices location settings NSString* url = SYSTEM_VERSION_LESS_THAN(@"10.0") ? @"prefs:root=LOCATION_SERVICES" : @"App-Prefs:root=Privacy&path=LOCATION"; [[UIApplication sharedApplication] openURL:[NSURL URLWithString: url]]; } else if (alertView.tag == 200) { //This will opne particular app location settings [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]]; } } else if(buttonIndex == 1)//Cancel button pressed. { //TODO for cancel } } 是一个有根的子树,基本上匹配所有内容。见https://golang.org/pkg/net/http/#ServeMux。 (在做出任何假设之前,请务必查阅文档。)