http.HandleFunc()HandleFunc()/favico.ico/images/ServeMux
/
例如:
func main() {
http.HandleFunc("/", route) // Match everything
http.ListenAndServe(":8080", nil)
}
var rNum = regexp.MustCompile(`\d`) // Has digit(s)
var rAbc = regexp.MustCompile(`abc`) // Contains "abc"
func route(w http.ResponseWriter, r *http.Request) {
switch {
case rNum.MatchString(r.URL.Path):
digits(w, r)
case rAbc.MatchString(r.URL.Path):
abc(w, r)
default:
w.Write([]byte("Unknown Pattern"))
}
}
func digits(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Has digits"))
}
func abc(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Has abc"))
}
或者使用像 Gorilla MUX 这样的外部库。