Go 上下文超时请求
示例
1.7+
使用上下文超时HTTP请求只能使用1.7+中的标准库(而不是子仓库)来完成:
import (
"context"
"net/http"
"time"
)
req, err := http.NewRequest("GET", `https://example.net`, nil)
ctx, _ := context.WithTimeout(context.TODO(), 200 * time.Milliseconds)
resp, err := http.DefaultClient.Do(req.WithContext(ctx))
//确保处理错误。
defer resp.Body.Close()1.7之前
import (
"net/http"
"time"
"golang.org/x/net/context"
"golang.org/x/net/context/ctxhttp"
)
ctx, err := context.WithTimeout(context.TODO(), 200 * time.Milliseconds)
resp, err := ctxhttp.Get(ctx, http.DefaultClient, "https://www.example.net")
//确保处理错误。
defer resp.Body.Close()进一步阅读
有关该context软件包的更多信息,请参见上下文。
热门推荐
1 泰迪狗狗如何训练
3 狗狗训练图
5 狗狗训练方法
6 训练狗狗不咬人
8 敏捷训练狗狗
10 训练狗狗吃饭
11 泰迪狗狗训练
12 狗狗从多大开始训练
13 狗狗训练学校费用
14 怎么去训练狗狗
15 狗狗训练狗狗
16 狗狗训练教程
17 训练狗狗装死
18 泰迪狗狗的训练方法