Go-标准库-context(二)
2023-06-13 09:18:47 时间
传递值
除了用于传递取消信号外,context还可以用于传递请求范围的值。可以通过context.WithValue函数将一个值保存到context中,然后在需要的地方通过context.Value函数获取该值。
下面是一个使用context传递请求范围的值的示例程序::
package main
import (
"context"
"fmt"
)
type key int
const (
nameKey key = iota
ageKey
)
func main() {
ctx := context.WithValue(context.Background(), nameKey, "Alice")
ctx = context.WithValue(ctx, ageKey, 20)
name, ok := ctx.Value(nameKey).(string)
if !ok {
fmt.Println("name not found")
return
}
age, ok := ctx.Value(ageKey).(int)
if !ok {
fmt.Println("age not found")
return
}
fmt.Printf("name: %s\n", name)
fmt.Printf("age: %d\n", age)
}
在这个示例程序中,我们通过context.WithValue函数将一个名为"name"的字符串和一个年龄为20的整数保存到context中。然后,我们分别通过context.Value函数获取这两个值,并将它们转换为相应的类型。最后,我们输出这两个值。
需要注意的是,使用context传递请求范围的值并不是一个好的设计,因为这样会让程序变得复杂,而且容易出错。如果需要在多个goroutine之间共享数据,应该使用同步原语(例如mutex或channel)来保证数据的安全性。
实际应用
context包被广泛用于Go中的网络编程和多线程编程中。在网络编程中,可以使用context包来传递请求的截止时间和取消信号,以便控制网络连接的超时和关闭。在多线程编程中,可以使用context包来控制多个goroutine之间的交互,避免出现竞争条件和死锁等问题。
下面是一个使用context包进行网络编程的示例程序:
package main
import (
"context"
"fmt"
"io/ioutil"
"net/http"
"time"
)
func main() {
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
req, err := http.NewRequest("GET", "https://www.google.com", nil)
if err != nil {
fmt.Println("http.NewRequest error:", err)
return
}
req = req.WithContext(ctx)
client := http.DefaultClient
resp, err := client.Do(req)
if err != nil {
fmt.Println("client.Do error:", err)
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("ioutil.ReadAll error:", err)
return
}
fmt.Println(string(body))
}
在这个示例程序中,我们创建了一个带有2秒超时的Cancel context,并将其作为请求的Context。然后,我们使用http.NewRequest函数创建了一个GET请求,并将请求的Context设置为上面创建的Cancel context。最后,我们使用http.DefaultClient执行请求,并读取响应的内容。
需要注意的是,在网络编程中,要注意合理设置请求的超时时间,避免请求超时或一直等待的情况发生。
相关文章
- Go数组和切片
- 【一Go到底】第一天---初识Goooooooooooooooooooooooo
- 2022-10-14:以下go语言代码输出什么?A:0;B:7;C:9;D:不能编译。package mainimport "f
- Go复合数据类型数组和切片
- Go 泛型
- 「Go工具箱」推荐一个Google开发的将结构体快速拼接成url查询参数的工具
- GO语言开篇-Go语言急速入门(基础知识点)| 青训营笔记
- autMan对接go-cqHttp详细教程
- Go-标准库-bufio(一)
- Go-标准库-sort(三)
- Go-标准库-testing(二)
- Go-标准库-testing(四)
- Go-标准库-text(三)
- Go-标准库-context(一)
- Go语言的性能如何?
- Go语言匿名函数——没有函数名字的函数
- Go语言内存管理简述
- 环境Go 编译 在 Linux 环境中的体验(go编译linux)
- Go语言连接Oracle数据库实战(go连接oracle)
- 轻松搞定Go语言连接MySQL(go连接mysql)
- 简易教程:如何在 Linux 上安装 GO 语言(go语言安装linux)
- Go语言环境配置:在 Linux 下实现(go环境搭建linux)
- 以Go语言操作MySQL:轻松运行数据库各种查询(go语言mysql)
- 学习Oracle Go了解信息技术世界(oracle go的用法)
- 小米与 Oculus 合作,将推出中国版 Oculus Go | CES 2018