go 的context 使用

2022/5/4 23:43:47

本文主要是介绍go 的context 使用,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

1, context.WithCancel

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

2, context.WithTimeout

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

3, context.WithValue

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


这篇关于go 的context 使用的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程