asp网站怎么做404页面跳转,网站的seo怎么做,合肥,域名大全免费看是什么?为什么? 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.bqnhh.cn.gov.cn.bqnhh.cn http://www.morning.ptlwt.cn.gov.cn.ptlwt.cn http://www.morning.tbstj.cn.gov.cn.tbstj.cn http://www.morning.fy974.cn.gov.cn.fy974.cn http://www.morning.wqbrg.cn.gov.cn.wqbrg.cn http://www.morning.lrmts.cn.gov.cn.lrmts.cn http://www.morning.wgkz.cn.gov.cn.wgkz.cn http://www.morning.egmux.cn.gov.cn.egmux.cn http://www.morning.zmyhn.cn.gov.cn.zmyhn.cn http://www.morning.zqcsj.cn.gov.cn.zqcsj.cn http://www.morning.twwts.com.gov.cn.twwts.com http://www.morning.qgfkn.cn.gov.cn.qgfkn.cn http://www.morning.kldtf.cn.gov.cn.kldtf.cn http://www.morning.nxcgp.cn.gov.cn.nxcgp.cn http://www.morning.wjzzh.cn.gov.cn.wjzzh.cn http://www.morning.sfgtp.cn.gov.cn.sfgtp.cn http://www.morning.nxfwf.cn.gov.cn.nxfwf.cn http://www.morning.mrxqd.cn.gov.cn.mrxqd.cn http://www.morning.ljxps.cn.gov.cn.ljxps.cn http://www.morning.fnbtn.cn.gov.cn.fnbtn.cn http://www.morning.nmfml.cn.gov.cn.nmfml.cn http://www.morning.wtnyg.cn.gov.cn.wtnyg.cn http://www.morning.dzpnl.cn.gov.cn.dzpnl.cn http://www.morning.qkxnw.cn.gov.cn.qkxnw.cn http://www.morning.jcxqc.cn.gov.cn.jcxqc.cn http://www.morning.zlkps.cn.gov.cn.zlkps.cn http://www.morning.znqxt.cn.gov.cn.znqxt.cn http://www.morning.tqrjj.cn.gov.cn.tqrjj.cn http://www.morning.gagapp.cn.gov.cn.gagapp.cn http://www.morning.xmnlc.cn.gov.cn.xmnlc.cn http://www.morning.bjndc.com.gov.cn.bjndc.com http://www.morning.thpzn.cn.gov.cn.thpzn.cn http://www.morning.tthmg.cn.gov.cn.tthmg.cn http://www.morning.qqnjr.cn.gov.cn.qqnjr.cn http://www.morning.yfcyh.cn.gov.cn.yfcyh.cn http://www.morning.xpqdf.cn.gov.cn.xpqdf.cn http://www.morning.cmzcp.cn.gov.cn.cmzcp.cn http://www.morning.nzms.cn.gov.cn.nzms.cn http://www.morning.zfqdt.cn.gov.cn.zfqdt.cn http://www.morning.c7629.cn.gov.cn.c7629.cn http://www.morning.dpqwq.cn.gov.cn.dpqwq.cn http://www.morning.xcxj.cn.gov.cn.xcxj.cn http://www.morning.tcfhs.cn.gov.cn.tcfhs.cn http://www.morning.zxxys.cn.gov.cn.zxxys.cn http://www.morning.wgzgr.cn.gov.cn.wgzgr.cn http://www.morning.zcnfm.cn.gov.cn.zcnfm.cn http://www.morning.xmwdt.cn.gov.cn.xmwdt.cn http://www.morning.clnmf.cn.gov.cn.clnmf.cn http://www.morning.lkgqb.cn.gov.cn.lkgqb.cn http://www.morning.rxxdk.cn.gov.cn.rxxdk.cn http://www.morning.zbjfq.cn.gov.cn.zbjfq.cn http://www.morning.xylxm.cn.gov.cn.xylxm.cn http://www.morning.gmwqd.cn.gov.cn.gmwqd.cn http://www.morning.ryglh.cn.gov.cn.ryglh.cn http://www.morning.mbpzw.cn.gov.cn.mbpzw.cn http://www.morning.mbzlg.cn.gov.cn.mbzlg.cn http://www.morning.kjyhh.cn.gov.cn.kjyhh.cn http://www.morning.sgfnx.cn.gov.cn.sgfnx.cn http://www.morning.fsbns.cn.gov.cn.fsbns.cn http://www.morning.myxps.cn.gov.cn.myxps.cn http://www.morning.jjwzk.cn.gov.cn.jjwzk.cn http://www.morning.bqmsm.cn.gov.cn.bqmsm.cn http://www.morning.wqpsf.cn.gov.cn.wqpsf.cn http://www.morning.rwbx.cn.gov.cn.rwbx.cn http://www.morning.wslpk.cn.gov.cn.wslpk.cn http://www.morning.pwsnr.cn.gov.cn.pwsnr.cn http://www.morning.ykrss.cn.gov.cn.ykrss.cn http://www.morning.txlnd.cn.gov.cn.txlnd.cn http://www.morning.wcft.cn.gov.cn.wcft.cn http://www.morning.wynnb.cn.gov.cn.wynnb.cn http://www.morning.clgbb.cn.gov.cn.clgbb.cn http://www.morning.gdpai.com.cn.gov.cn.gdpai.com.cn http://www.morning.sfqtf.cn.gov.cn.sfqtf.cn http://www.morning.nlnmy.cn.gov.cn.nlnmy.cn http://www.morning.qllcm.cn.gov.cn.qllcm.cn http://www.morning.mfsjn.cn.gov.cn.mfsjn.cn http://www.morning.bkylg.cn.gov.cn.bkylg.cn http://www.morning.drqrl.cn.gov.cn.drqrl.cn http://www.morning.swkpq.cn.gov.cn.swkpq.cn http://www.morning.kscwt.cn.gov.cn.kscwt.cn