Golang 1.7.3 Context 简单用法.类似sync.WaitGroup

来源:互联网 发布:短信数据恢复软件 编辑:程序博客网 时间:2024/04/30 08:00
package mainimport (    "context"    "fmt"    "time")func main() {    ctx, cancelFunc := context.WithDeadline(context.Background(), time.Now().Add(time.Second*10))    t, ok := ctx.Deadline()    if ok {        fmt.Println(time.Now())        fmt.Println(t.String())    }    go func(ctx context.Context) {        fmt.Println(ctx.Value("Test"))        <-ctx.Done()        fmt.Println(ctx.Err())    }(ctx)    if ctx.Err() == nil {        time.Sleep(11e9)    }    if ctx.Err() != nil {        fmt.Println("已经退出了")    }    cancelFunc()}
1 0