package main import ( "context" "fmt" "time" ) func main() { test() time.Sleep(time.Second * 5) } func test() { ctx, cancel := context.WithCancel(context.Background()) defer cancel() intChan := goroute(ctx) for n := range intChan { fmt.Println(n) if n == 5 { break } } } func goroute(ctx context.Context) <-chan int { chanInt := make(chan int) n := 1 go func() { for { select { case <-ctx.Done(): fmt.Println("exited") return case chanInt <- n: n++ } } }() return chanInt }
输出:
1 2 3 4 5 exited
package main import ( "context" "fmt" "io/ioutil" "net/http" "time" ) type Result struct { resp *http.Response err error } func main() { process() } func process() { ctx, cancel := context.WithTimeout(context.Background(), time.Second*2) defer cancel() tr := &http.Transport{} client := &http.Client{Transport: tr} chanResp := make(chan Result, 1) req, err := http.NewRequest("GET", "http://www.google.com", nil) if err != nil { fmt.Println("请求失败,err:", err) return } go func() { resp, err := client.Do(req) pack := Result{ resp: resp, err: err, } chanResp <- pack }() select { case <-ctx.Done(): tr.CancelRequest(req) res := <-chanResp fmt.Println("请求超时,err:", res.err) case res := <-chanResp: defer res.resp.Body.Close() out, _ := ioutil.ReadAll(res.resp.Body) fmt.Println("请求成功:") fmt.Println(out) } }
输出:
请求超时,err: Get "http://www.google.com": net/http: request canceled while waiting for connection
package main import ( "context" "fmt" ) func main() { ctx := context.WithValue(context.Background(), "trace_id", 12345678) ctx = context.WithValue(ctx, "session", "asdfjladjf") process(ctx) } func process(ctx context.Context) { ret, ok := ctx.Value("trace_id").(int) if !ok { fmt.Println("获取ctx的trace_id失败") return } fmt.Println("获取ctx的trace_id:", ret) s, _ := ctx.Value("session").(string) fmt.Println("获取ctx的session:", s) }
输出:
获取ctx的trace_id: 12345678 获取ctx的session: asdfjlad