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软件包的更多信息,请参见上下文。
热门推荐
3 祝福语简短的微信
10 幼儿祝福语大全简短贺卡
11 感谢教官文案祝福语简短
12 鼠年同事祝福语简短创意
13 元旦祝福语 简短独特群发
14 妈咪跟我生日祝福语简短
15 给儿子新婚祝福语简短
16 伴娘祝福语长文案简短
17 恭喜高考完祝福语简短
18 生日祝福语送老公简短