package main
import (
"fmt"
"net/http"
)
func middleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Println("前置操作")
next.ServeHTTP(w, r)
fmt.Println("后置操作")
})
}
func root(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.Write([]byte(`{"name":"SuperWang","hobbies":["football","programming"]}`))
}
func main() {
http.Handle("/jump", http.RedirectHandler("http://www.baidu.com", 302))
http.Handle("/", middleware(http.HandlerFunc(root)))
err := http.ListenAndServe(":8080", nil)
if err != nil {
fmt.Println(err)
}
}
golang 中间件、json返回、302跳转
猜你喜欢
转载自blog.csdn.net/hjmnasdkl/article/details/81286723
今日推荐
周排行