本文开始将针对context的用法进行系统化讨论,在这里你将可以在工做中合理使用context解决一些比较棘手的问题。golang
context处理超时处理以外还能够用来保存数据,当你须要在多个上下文传递时传递数据,那么本文提到的知识能够排上用场。算法
示例代码为一个简单的http服务,流程是登陆以后会跳转首页,首页经过guard中间件进行鉴权。固然,示例代码未作其余诸如链接数据库之类的处理,这不是本文的重点。
守卫函数读取cookie以后将cookie值写入context并向下传递,在整个请求中能够说是“透明”的。当访问到须要保护的接口时检测到没有提供cookie,则直接终端请求,不然经过r.WithContext将username的值存入cookie,避免的业务接口直接读取cookie的弊端。由于若是后期更改鉴权算法的话,业务代码能够不用更改,直接更改中间件便可。数据库
package main import ( "context" "fmt" "log" "net/http" "time" ) func main() { mux := http.NewServeMux() mux.HandleFunc("/", guard(home)) mux.HandleFunc("/login", login) log.Fatal(http.ListenAndServe(":8080", mux)) } // 登陆 func login(w http.ResponseWriter, r *http.Request) { if r.URL.Query().Get("username") != "root" { http.Error(w, http.StatusText(401), 401) return } cookie := &http.Cookie{Name: "username", Value: "root", Expires: time.Now().Add(time.Hour)} http.SetCookie(w, cookie) http.Redirect(w, r, "/", 302) } func home(w http.ResponseWriter, r *http.Request) { username := r.Context().Value("username") fmt.Fprintf(w, "welcome login: %s", username.(string)) } // 守卫 func guard(handleFunc http.HandlerFunc) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { // check username log.Printf("%s - %s\n", r.Method, r.RequestURI) cookie, err := r.Cookie("username") if err != nil || cookie == nil { // 若是username为空直接拦截 http.Error(w, http.StatusText(401), 401) return } handleFunc(w, r.WithContext(context.WithValue(r.Context(), "username", cookie.Value))) } }
本文的代码就这么多,内容也不多,但愿你们能好好用上这个利器。
关于context与协程超时控制将在下一篇文章中讲到。cookie
天天进步一点点,欢迎你们做客!函数