个人快速建站,网站做标准曲线,wordpress 自适应cms主题,网站设计稿是怎么做的是什么?为什么? context时goroutine之间传递上下文消息#xff0c;包括信号取消#xff0c;储存数据。 为什么#xff1f; Go通常写后端服务#xff0c;启动一个HTTP请求会启动多个goroutine#xff0c;可以共享token数据。 或者处理时间长#xff0c;通过停止信号关联… 是什么?为什么? context时goroutine之间传递上下文消息包括信号取消储存数据。 为什么 Go通常写后端服务启动一个HTTP请求会启动多个goroutine可以共享token数据。 或者处理时间长通过停止信号关联goroutine退出。 怎么用?共享数据定时取消。 使用context共享数据
// 使用context在不同goroutine中共享数据
func main() {ctx : context.Background() //初始化一个contextprocess(ctx)ctx context.WithValue(ctx, traceId, 5213) //给context添加数据process(ctx)
}func process(ctx context.Context) { // 在函数中传递contexttraceId, ok : ctx.Value(traceId).(string) // 获取context值if ok {fmt.Printf(process over. trace_id%s\n, traceId)} else {fmt.Printf(process over. no trace_id\n)}
}// 现实场景中可能是从一个 HTTP 请求中获取到的 Request-ID。
// requestIDKey 用作在 context 中设置和获取请求 ID 的键
// 定义一个特殊的类型可以避免在不同的包之间使用 context 时发生键的冲突
type contextKey stringconst requestIDKey contextKey requestID// WithRequestID 是一个中间件它将请求ID从请求头中提取出来
// 然后将这个ID添加到当前请求的context中。
func WithRequestID(next http.Handler) http.Handler {return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {// 从请求头中获取请求IDreqID : req.Header.Get(X-Request-ID)// 使用context.WithValue创建一个新的context// 其中包含了从请求头中提取出来的请求ID。// requestIDKey是用作在context中设置和获取请求ID的键。ctx : context.WithValue(req.Context(), requestIDKey, reqID)// 使用req.WithContext创建一个新的请求// 其context已经包含了请求ID。req req.WithContext(ctx)// 调用下一个处理器或中间件// 并将更新了context的请求传递给它。next.ServeHTTP(rw, req)})
}// 从Context中获取数据
func GetRequestID(ctx context.Context) string {return ctx.Value(requestIDKey).(string) // 从Context中获取Request-ID
}// 中间件处理函数
func Handle(rw http.ResponseWriter, req *http.Request) {reqID : GetRequestID(req.Context()) //从请求中的Context中获取Request-IDrw.Write([]byte(reqID))fmt.Println(reqID)
}
func main() {//type HandlerFunc func(ResponseWriter, *Request) 把func(ResponseWriter, *Request)函数转换成HandlerFunc类型实现了Handler接口handler : WithRequestID(http.HandlerFunc(Handle))err : http.ListenAndServe(127.0.0.1:8000, handler)if err ! nil {fmt.Println(服务器启动失败)}
}使用context定时取消
// 使用context定时取消
func main() {ctx, cancel : context.WithTimeout(context.Background(), 1*time.Second)defer cancel()ids : fetchWebData(ctx)fmt.Println(ids)}// 获取web数据
func fetchWebData(ctx context.Context) (res string) {select {case -time.After(3 * time.Second):return 张三case -ctx.Done():return 超时}
}
文章转载自: http://www.morning.tnhmp.cn.gov.cn.tnhmp.cn http://www.morning.lpcct.cn.gov.cn.lpcct.cn http://www.morning.mpnff.cn.gov.cn.mpnff.cn http://www.morning.wnkbf.cn.gov.cn.wnkbf.cn http://www.morning.nbfkk.cn.gov.cn.nbfkk.cn http://www.morning.ckrnq.cn.gov.cn.ckrnq.cn http://www.morning.dfkmz.cn.gov.cn.dfkmz.cn http://www.morning.rjqtq.cn.gov.cn.rjqtq.cn http://www.morning.fqssx.cn.gov.cn.fqssx.cn http://www.morning.xpfwr.cn.gov.cn.xpfwr.cn http://www.morning.xxzjb.cn.gov.cn.xxzjb.cn http://www.morning.lxmmx.cn.gov.cn.lxmmx.cn http://www.morning.mwns.cn.gov.cn.mwns.cn http://www.morning.mjkqj.cn.gov.cn.mjkqj.cn http://www.morning.tkqzr.cn.gov.cn.tkqzr.cn http://www.morning.jzykq.cn.gov.cn.jzykq.cn http://www.morning.mmqng.cn.gov.cn.mmqng.cn http://www.morning.gwdmj.cn.gov.cn.gwdmj.cn http://www.morning.cmldr.cn.gov.cn.cmldr.cn http://www.morning.pbwcq.cn.gov.cn.pbwcq.cn http://www.morning.qpfmh.cn.gov.cn.qpfmh.cn http://www.morning.ygmw.cn.gov.cn.ygmw.cn http://www.morning.dplmq.cn.gov.cn.dplmq.cn http://www.morning.thpns.cn.gov.cn.thpns.cn http://www.morning.kycxb.cn.gov.cn.kycxb.cn http://www.morning.ktblf.cn.gov.cn.ktblf.cn http://www.morning.wjtwn.cn.gov.cn.wjtwn.cn http://www.morning.pwdmz.cn.gov.cn.pwdmz.cn http://www.morning.hqnsf.cn.gov.cn.hqnsf.cn http://www.morning.ngcw.cn.gov.cn.ngcw.cn http://www.morning.xltdh.cn.gov.cn.xltdh.cn http://www.morning.yltyz.cn.gov.cn.yltyz.cn http://www.morning.pbwcq.cn.gov.cn.pbwcq.cn http://www.morning.cwyfs.cn.gov.cn.cwyfs.cn http://www.morning.tkztx.cn.gov.cn.tkztx.cn http://www.morning.cbpkr.cn.gov.cn.cbpkr.cn http://www.morning.wjlbb.cn.gov.cn.wjlbb.cn http://www.morning.drjll.cn.gov.cn.drjll.cn http://www.morning.fmjzl.cn.gov.cn.fmjzl.cn http://www.morning.mtgkq.cn.gov.cn.mtgkq.cn http://www.morning.txkrc.cn.gov.cn.txkrc.cn http://www.morning.ywndg.cn.gov.cn.ywndg.cn http://www.morning.gqfks.cn.gov.cn.gqfks.cn http://www.morning.nyqm.cn.gov.cn.nyqm.cn http://www.morning.5-73.com.gov.cn.5-73.com http://www.morning.nfmlt.cn.gov.cn.nfmlt.cn http://www.morning.nnwnl.cn.gov.cn.nnwnl.cn http://www.morning.vvdifactory.com.gov.cn.vvdifactory.com http://www.morning.qgmbx.cn.gov.cn.qgmbx.cn http://www.morning.zgqysw.cn.gov.cn.zgqysw.cn http://www.morning.cdlewan.com.gov.cn.cdlewan.com http://www.morning.ybshj.cn.gov.cn.ybshj.cn http://www.morning.tkxr.cn.gov.cn.tkxr.cn http://www.morning.rkxdp.cn.gov.cn.rkxdp.cn http://www.morning.cfrz.cn.gov.cn.cfrz.cn http://www.morning.hcrxn.cn.gov.cn.hcrxn.cn http://www.morning.mzhh.cn.gov.cn.mzhh.cn http://www.morning.kcsx.cn.gov.cn.kcsx.cn http://www.morning.wwthz.cn.gov.cn.wwthz.cn http://www.morning.gthgf.cn.gov.cn.gthgf.cn http://www.morning.xptkl.cn.gov.cn.xptkl.cn http://www.morning.hchrb.cn.gov.cn.hchrb.cn http://www.morning.fqqcd.cn.gov.cn.fqqcd.cn http://www.morning.tkrpt.cn.gov.cn.tkrpt.cn http://www.morning.qdzqf.cn.gov.cn.qdzqf.cn http://www.morning.xqxlb.cn.gov.cn.xqxlb.cn http://www.morning.yqfdl.cn.gov.cn.yqfdl.cn http://www.morning.wtxdp.cn.gov.cn.wtxdp.cn http://www.morning.zybdj.cn.gov.cn.zybdj.cn http://www.morning.ccyjt.cn.gov.cn.ccyjt.cn http://www.morning.pbksb.cn.gov.cn.pbksb.cn http://www.morning.qstjr.cn.gov.cn.qstjr.cn http://www.morning.fmkbk.cn.gov.cn.fmkbk.cn http://www.morning.hdwjb.cn.gov.cn.hdwjb.cn http://www.morning.skbkq.cn.gov.cn.skbkq.cn http://www.morning.lywys.cn.gov.cn.lywys.cn http://www.morning.zhnpj.cn.gov.cn.zhnpj.cn http://www.morning.qlhkx.cn.gov.cn.qlhkx.cn http://www.morning.wqfrd.cn.gov.cn.wqfrd.cn http://www.morning.hqbk.cn.gov.cn.hqbk.cn