在Go 中使用正規表示式進行URL 模式比對
問題:
問題:問題: 問題:
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")) }
以上是如何在 Go 中使用正規表示式路由 URL?的詳細內容。更多資訊請關注PHP中文網其他相關文章!