Go 1.22中一个很大的变化是http.ServeMux中的模式匹配,先前这个功能是很多第三方的web框架或者router库实现的。我们很有必要好好研究它,将来在实现HTTP API的时候可能优先使用它。http.ServeMux是一个HTTP请求多路复用器,它将每个传入请求的URL与已注册的模式列表进行匹配,并调用与最接近URL匹配的模式对应的处理程序。比如:1234567891011121314151617mux.HandleFunc("POST /items/create",func(w http.ResponseWriter, r *http.Request) {fmt.Fprint(w,"POST item created")})mux.HandleFunc("/items/create",func(w http.ResponseWriter, r *http.Request) {fmt.Fprint(w,"item created")})mux.HandleFunc("/items/{id}",func(w http.ResponseWriter, r *http.Request) {id := r.PathValue("id")fmt.Fprintf(w,"Item ID = %s", id)})mux.HandleFunc("/files/{path...}",f
...
继续阅读
(153)