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软件包的更多信息,请参见上下文。
热门推荐
10 八一幼儿祝福语大全简短
11 公司乔迁食堂祝福语简短
12 婚礼结束聚餐祝福语简短
13 儿媳买车妈妈祝福语简短
14 毕业送礼老师祝福语简短
15 同事辞职正常祝福语简短
16 恭贺新婚文案祝福语简短
17 金店立秋祝福语简短英文
18 婆婆高寿祝福语大全简短