context.withcancel 方法可以创建一个子上下文,它可以通过 parent 上下文中的 cancelfunc 函数取消。语法:func withcancel(parent context) (ctx context, cancel cancelfunc),取消子上下文的函数是 cancel。它可以用来取消 http 请求和 goroutine,示例如下:1) 取消 http 请求:使用子上下文设置请求,并在超时时调用取消函数;2) 取消 goroutine:在子上下文中启动 goroutine,并在需要时调用取消函数。

Go 语言函数:使用 context.WithCancel 创建可取消的子上下文
在 Go 语言中,context.Context 类型提供了请求相关元数据和取消功能。context.WithCancel 函数允许创建一个新的子上下文,该上下文可以被父上下文取消。
语法:
立即学习“go语言免费学习笔记(深入)”;
func WithCancel(parent Context) (ctx Context, cancel CancelFunc)
parent:父上下文的源上下文。ctx:返回的新子上下文。cancel:取消子上下文的函数。使用案例:
示例 1:使用子上下文取消 HTTP 请求
以下示例演示如何使用 context.WithCancel 在超时的情况下取消 HTTP 请求:
package main
import (
"context"
"fmt"
"net/http"
)
func main() {
// 创建父上下文和取消函数
ctx, cancel := context.WithCancel(context.Background())
// 创建 HTTP 客户端
client := http.Client{
Timeout: 5 * time.Second,
}
// 发起 HTTP 请求
req, err := http.NewRequest(http.MethodGet, "https://example.com", nil)
if err != nil {
fmt.Printf("无法创建请求: %v\n", err)
return
}
req = req.WithContext(ctx)
// 发送请求,如果有必要,取消请求
resp, err := client.Do(req)
if err != nil {
fmt.Printf("请求出错: %v\n", err)
if err == context.Canceled {
fmt.Printf("请求已取消,超时时间为 5 秒\n")
}
return
}
// 请求成功,只需打印响应即可
fmt.Printf("请求成功:\n")
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Printf("无法读取响应体:%v\n", err)
}
fmt.Printf("%s\n", body)
// 取消上下文以释放任何资源
cancel()
}在上述示例中:
context.Background() 中创建。context.WithCancel 函数创建一个子上下文,该上下文可以在超时后取消。req.WithContext(ctx) 设置子上下文。client.Do(req) 通过调用取消函数在请求超时时取消该请求。cancel() 函数被调用以释放与子上下文关联的任何资源。示例 2:取消 Goroutine
以下示例演示如何使用 context.WithCancel 取消 goroutine:
package main
import (
"context"
"fmt"
"time"
)
func main() {
// 创建父上下文和取消函数
ctx, cancel := context.WithCancel(context.Background())
// 启动一个 goroutine 并将其传递子上下文
go func(ctx context.Context) {
for {
select {
case <-ctx.Done():
fmt.Println("goroutine 已取消")
return
default:
// 继续执行代码...
}
}
}(ctx)
time.Sleep(5 * time.Second)
cancel()
}在上述示例中:
goroutine 在子上下文中启动,以响应取消信号。time.Sleep 函数将程序挂起 5 秒,这给了 goroutine 足够的时间在取消时响应。cancel() 调用将取消子上下文,从而也取消 goroutine。以上就是Golang 函数:用 context.WithCancel 创建可取消的子上下文的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号