context

 1 package main
 2 
 3 import (
 4     "context"
 5     "fmt"
 6     "time"
 7 )
 8 
 9 var key string = "name"
10 
11 func main() {
12     ctx, cancel := context.WithCancel(context.Background())
13 
14     valueCtx := context.WithValue(ctx, key, "add value")
15 
16     go watch(valueCtx)
17     time.Sleep(10 * time.Second)
18     cancel()
19 
20     time.Sleep(5 * time.Second)
21 }
22 
23 func watch(ctx context.Context) {
24     for {
25         select {
26         case <-ctx.Done():
27             //get value
28             fmt.Println(ctx.Value(key), "is cancel")
29 
30             return
31         default:
32             //get value
33             fmt.Println(ctx.Value(key), "int goroutine")
34 
35             time.Sleep(2 * time.Second)
36         }
37     }
38 }

 

注释掉最后一行后

 

 

 这样改

 

 1 package main
 2 
 3 import (
 4     "context"
 5     "log"
 6     "os"
 7     "time"
 8 )
 9 
10 var logg *log.Logger
11 
12 func someHandler() {
13     // 新建一个ctx
14     ctx, cancel := context.WithCancel(context.Background())
15 
16     //传递ctx
17     go doStuff(ctx)
18 
19     //10秒后取消doStuff
20     time.Sleep(10 * time.Second)
21     log.Println("cancel")
22 
23     //调用cancel:context.WithCancel 返回的CancelFunc
24     cancel()
25 
26 }
27 
28 func doStuff(ctx context.Context) {
29 
30     // for 循环来每1秒work一下,判断ctx是否被取消了,如果是就退出
31 
32     for {
33         time.Sleep(1 * time.Second)
34 
35         select {
36         case <-ctx.Done():
37             logg.Printf("done")
38             return
39         default:
40             logg.Printf("work")
41         }
42     }
43 }
44 
45 func main() {
46     logg = log.New(os.Stdout, "", log.Ltime)
47     someHandler()
48     logg.Printf("down")
49 }

 

 1 package main
 2 
 3 import (
 4     "context"
 5     "log"
 6     "os"
 7     "time"
 8 )
 9 
10 var logg *log.Logger
11 
12 func doTimeOutStuff(ctx context.Context) {
13     for {
14         time.Sleep(1 * time.Second)
15 
16         if deadline, ok := ctx.Deadline(); ok { //设置了deadl
17             logg.Printf("deadline set")
18             if time.Now().After(deadline) {
19                 logg.Printf(ctx.Err().Error())
20                 return
21             }
22 
23         }
24 
25         select {
26         case <-ctx.Done():
27             logg.Printf("done")
28             return
29         default:
30             logg.Printf("work")
31         }
32     }
33 }
34 
35 func timeoutHandler() {
36     ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
37     // ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(5*time.Second))
38     go doTimeOutStuff(ctx)
39     // go doStuff(ctx)
40 
41     time.Sleep(10 * time.Second)
42 
43     cancel()
44 
45 }
46 
47 func main() {
48     logg = log.New(os.Stdout, "", log.Ltime)
49     timeoutHandler()
50     logg.Printf("down")
51 }

 

 

 

 

 1 package main
 2 
 3 import (
 4     "context"
 5     "fmt"
 6 )
 7 
 8 type favContextKey string
 9 
10 func main() {
11     f := func(ctx context.Context, key favContextKey) {
12         if v := ctx.Value(key); v != nil {
13             fmt.Println("found value:", v)
14             return
15         }
16         fmt.Println("key not found:", key)
17     }
18     key := favContextKey("language")
19     ctx := context.WithValue(context.Background(), key, "Go")
20     // func WithValue(parent Context, key, val interface{}) Context
21     f(ctx, key)
22     f(ctx, favContextKey("color"))
23 }  
posted @ 2019-12-11 21:45  尘归风  阅读(782)  评论(0)    收藏  举报