当前位置: 首页 > news >正文

用公司的信息做网站违法吗郑州网站建设 华数

用公司的信息做网站违法吗,郑州网站建设 华数,开发者社区,wordpress使用自己主页Context 介绍 Context 代表了协程的上下文,用以在父子协程之间传递控制信号,共享变量等操作// context.Context 接口 type Context interface {// 当Context自动取消或者到了取消时间被取消后返回Deadline() (deadline time.Time, ok bool)// 当Contex…

Context

介绍

Context 代表了协程的上下文,用以在父子协程之间传递控制信号,共享变量等操作// context.Context 接口
type Context interface {// 当Context自动取消或者到了取消时间被取消后返回Deadline() (deadline time.Time, ok bool)// 当Context被取消或者到了deadline返回一个被关闭的channelDone() <-chan struct{}// 当Context被取消或者关闭后,返回context取消的原因Err() error// 获取 Context 中保存的键值对数据Value(key any) any
}

使用 WithValue() 传递数据

func A(ctx context.Context) context.Context {time.Sleep(1 * time.Second)fmt.Println("A: ", ctx.Value("main"))ctx = context.WithValue(ctx, "A", "A-1")go B(ctx)return ctx
}func B(ctx context.Context) context.Context {time.Sleep(1 * time.Second)fmt.Println("B: ", ctx.Value("main"))fmt.Println("B: ", ctx.Value("A"))return ctx
}func main() {ctx := context.WithValue(context.Background(), "main", "main-1")go A(ctx)time.Sleep(3 * time.Second)
}

使用 WithCancel() 取消操作

func main() {ctx, cancel := context.WithCancel(context.Background())go Speak(ctx)time.Sleep(3 * time.Second)cancel()time.Sleep(1 * time.Second)
}func Speak(ctx context.Context) {for range time.Tick(time.Second) {select {case <-ctx.Done():fmt.Println("我要闭嘴了")returndefault:fmt.Println("balabalabalabala")}}
}

使用 WithDeadline() 设置截止时间

使用 WithTimeout() 设置超时时间

withTimeout和withDeadline作用是一样的,就是传递的时间参数不同,会通过传入的时间来自动取消Context,都会返回一个cancelFunc方法,通过调用这个方法可以达到提前进行取消
使用的过程还是建议在自动取消后也调用cancelFunc去停止定时减少不必要的资源浪费
func A(in chan struct{}) {time.Sleep(1 * time.Second)in <- struct{}{}
}func B(in chan struct{}) {time.Sleep(3 * time.Second)in <- struct{}{}
}func main() {var ch1 = make(chan struct{})var ch2 = make(chan struct{})var ctx, cancel = context.WithTimeout(context.Background(), 2*time.Second)go func() {go A(ch1)select {case <-ctx.Done():fmt.Println("ctx timeout")breakcase <-ch1:fmt.Println("A Done")}}()go func() {go B(ch2)select {case <-ctx.Done():fmt.Println("ctx timeout")breakcase <-ch2:fmt.Println("B Done")}}()defer cancel()time.Sleep(5 * time.Second)
}
http://www.15wanjia.com/news/197088.html

相关文章:

  • 网站建设的6个基本步骤做彩票网站捉怎么处理
  • 平台网站建设有哪些国外做的好的鲜花网站
  • 网页设计中界面设计尺寸为seo引擎优化是做什么的
  • 廊坊网站建设费用泰安工作招聘
  • 青海省公路建设管理局门户网站wordpress忘了秘密
  • 分类目录网站怎么做wordpress后台怎么进的
  • 网站建站解决方案html5手机网站开发教程
  • 四川省建设厅注册管理中心网站首页wordpress怎么改登陆地址
  • 局网站建设方案word为什么南极建站在2月
  • 建网站买服务器现货交易平台查询
  • 漳州seo建站公司网站建设有什么好处
  • 网站文章不显示建站系统哪个比较好
  • 社交网站 源码可以做微积分的网站
  • 网站建设 昆明邦凯网络毕业设计网站做啥
  • 长沙网站排名提升网络建设情况怎么填
  • 临沂企业建站手机一键优化
  • 德保网站建设什么网站做产品销售做的好
  • 宿州建设企业网站公司下载谷歌浏览器并安装
  • 做net网站网站开发官网源码
  • iis如何建立网站网站建设推广群
  • 定制网站建设广告普象工业设计网站
  • 网站建设开发三层架构淄博找能做网站的公司
  • 网站html地图模板网站建设优化400报价
  • 四川建设厅官方网站什么网站可以接单做设计方案
  • 众筹网站搭建谷歌seo排名优化
  • wap网站建设环保工程 技术支持 东莞网站建设
  • 响应式网站建设市场个人网站制作的步骤
  • 三角网站建设网站建设怎么设置网址
  • 网站双线主机优势51ppt模板网官网
  • 如果让你建设网站之前你会想什么珠海编程培训机构