Go Context

Context

介绍

// Context 代表了协程的上下文,用以在父子协程之间传递控制信号,共享变量等操作

// context.Context 接口
type Context interface {
	// 当Context自动取消或者到了取消时间被取消后返回
	Deadline() (deadline time.Time, ok bool)
	// 当Context被取消或者到了deadline返回一个被关闭的channel
	Done() <-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("我要闭嘴了")
			return
		default:
			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")
			break
		case <-ch1:
			fmt.Println("A Done")
		}
	}()

	go func() {
		go B(ch2)
		select {
		case <-ctx.Done():
			fmt.Println("ctx timeout")
			break
		case <-ch2:
			fmt.Println("B Done")
		}
	}()
	defer cancel()
	time.Sleep(5 * time.Second)
}
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值