goroutine上下文contxt语法

// gen generates integers in a separate goroutine and
// sends them to the returned channel.
// The callers of gen need to cancel the context once
// they are done consuming generated integers not to leak
// the internal goroutine started by gen.
//gen在一个单独的goroutine中生成整数并将它们发送到返回的通道。

//gen的调用方需要在使用生成的整数后取消上下文,以避免泄漏gen启动的内部goroutine。

context简单概述:

Go服务器的每个请求都有自己的goroutine,而有的请求为了提高性能,会经常启动额外的goroutine处理请求,当该请求被取消或超时,该请求上的所有goroutines应该退出,防止资源泄露。那么context来了,它对该请求上的所有goroutines进行约束,然后进行取消信号,超时等操作。而context优点就是简洁的管理goroutines的生命周期。
注意: 使用时遵循context规则

  1. 不要将 Context放入结构体,Context应该作为第一个参数传
    入,命名为ctx。
  2. 即使函数允许,也不要传入nil的 Context。如果不知道用哪种
    Context,可以使用context.TODO()。
  3. 使用context的Value相关方法,只应该用于在程序和接口中传递
    和请求相关数据,不能用它来传递一些可选的参数
  4. 相同的 Context 可以传递给在不同的goroutine;Context 是
    并发安全的。

`package main

import (
"context"
"fmt"
)
func main() {
gen := func(ctx context.Context) <-chan int {
dst := make(chan int)
n := 1
go func() {
for {
select {
case <-ctx.Done():
return // returning not to leak the goroutine
case dst <- n:
n++
}
}
}()
return dst
}

ctx, cancel := context.WithCancel(context.Background())
defer cancel() // cancel when we are finished consuming integers

for n := range gen(ctx) {
	fmt.Println(n)
	if n == 5 {
		break
	}
}

}`

结果:
1
2
3
4
5

posted @ 2020-08-26 10:57  刘大侠GG_B  阅读(175)  评论(0编辑  收藏  举报