Go_context包

是什么?为什么?

context时goroutine之间传递上下文消息,包括信号取消,储存数据。

为什么?

Go通常写后端服务,启动一个HTTP请求会启动多个goroutine,可以共享token数据。

或者处理时间长,通过停止信号关联goroutine退出。

怎么用?共享数据,定时取消。

使用context共享数据

Go 复制代码
// 使用context在不同goroutine中共享数据
func main() {
	ctx := context.Background() //初始化一个context
	process(ctx)
	ctx = context.WithValue(ctx, "traceId", "5213") //给context添加数据
	process(ctx)
}

func process(ctx context.Context) { // 在函数中传递context
	traceId, ok := ctx.Value("traceId").(string) // 获取context值
	if ok {
		fmt.Printf("process over. trace_id=%s\n", traceId)
	} else {
		fmt.Printf("process over. no trace_id\n")
	}
}
Go 复制代码
// 现实场景中可能是从一个 HTTP 请求中获取到的 Request-ID。
// requestIDKey 用作在 context 中设置和获取请求 ID 的键
// 定义一个特殊的类型可以避免在不同的包之间使用 context 时发生键的冲突
type contextKey string

const requestIDKey contextKey = "requestID"

// WithRequestID 是一个中间件,它将请求ID从请求头中提取出来,
// 然后将这个ID添加到当前请求的context中。
func WithRequestID(next http.Handler) http.Handler {
	return http.HandlerFunc(
		func(rw http.ResponseWriter, req *http.Request) {
			// 从请求头中获取请求ID
			reqID := req.Header.Get("X-Request-ID")

			// 使用context.WithValue创建一个新的context,
			// 其中包含了从请求头中提取出来的请求ID。
			// requestIDKey是用作在context中设置和获取请求ID的键。
			ctx := context.WithValue(req.Context(), requestIDKey, reqID)

			// 使用req.WithContext创建一个新的请求,
			// 其context已经包含了请求ID。
			req = req.WithContext(ctx)

			// 调用下一个处理器(或中间件),
			// 并将更新了context的请求传递给它。
			next.ServeHTTP(rw, req)
		})
}

// 从Context中获取数据
func GetRequestID(ctx context.Context) string {
	return ctx.Value(requestIDKey).(string) // 从Context中获取Request-ID
}

// 中间件处理函数
func Handle(rw http.ResponseWriter, req *http.Request) {
	reqID := GetRequestID(req.Context()) //从请求中的Context中获取Request-ID
	rw.Write([]byte(reqID))
	fmt.Println(reqID)
}
func main() {
	//type HandlerFunc func(ResponseWriter, *Request) 把func(ResponseWriter, *Request)函数转换成HandlerFunc类型实现了Handler接口
	handler := WithRequestID(http.HandlerFunc(Handle))
	err := http.ListenAndServe("127.0.0.1:8000", handler)
	if err != nil {
		fmt.Println("服务器启动失败")
	}
}

使用context定时取消

Go 复制代码
// 使用context定时取消 
func main() {
	ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
	defer cancel()
	ids := fetchWebData(ctx)
	fmt.Println(ids)

}

// 获取web数据
func fetchWebData(ctx context.Context) (res string) {
	select {
	case <-time.After(3 * time.Second):
		return "张三"
	case <-ctx.Done():
		return "超时"

	}
}
相关推荐
怪我冷i4 小时前
使用vscode调试wails项目(golang桌面GUI)
vscode·golang
小吴同学(wlx)7 小时前
Golang 进阶3—— 协程&管道
golang
技术卷7 小时前
GO网络编程(三):海量用户通信系统1:登录功能初步
golang·网络编程
虽千万人 吾往矣10 小时前
golang gorm
开发语言·数据库·后端·tcp/ip·golang
__AtYou__1 天前
Golang | Leetcode Golang题解之第448题找到所有数组中消失的数字
leetcode·golang·题解
千年死缓1 天前
go+redis基于tcp实现聊天室
redis·tcp/ip·golang
吃着火锅x唱着歌1 天前
Redis设计与实现 学习笔记 第五章 跳跃表
golang
技术卷1 天前
Redis数据库与GO完结篇:redis操作总结与GO使用redis
数据库·redis·golang
white.tie2 天前
vscode配置golang
ide·vscode·golang
陈序缘2 天前
Go语言实现长连接并发框架 - 任务管理器
linux·服务器·开发语言·后端·golang