golang http 请求添加拦截器
作者:互联网
//type HandlerFunc func(ResponseWriter, *Request)
拦截器返回一个函数供调用,在这个函数里添加自己的逻辑判断即可 h(w,r)及是调用用户自己的处理函数。h 是函数指针
func handleIterceptor(h http.HandlerFunc) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { fmt.Println("handleIterceptor") h(w, r) } } // / func myhandler(writer http.ResponseWriter, request *http.Request) { fmt.Println("myhandler") } func web(writer http.ResponseWriter, request *http.Request) { fmt.Println("web") } func main() { http.HandleFunc("/", handleIterceptor(myhandler)) http.HandleFunc("/web", handleIterceptor(web)) http.ListenAndServe(":8080", nil) }
标签:web,拦截器,http,Request,handleIterceptor,golang,func,ResponseWriter 来源: https://www.cnblogs.com/libing029/p/12483480.html