func GetFoo(ctx context.Context) (*T, error) {
req, err := http.NewRequest("GET", "http://foo/t.json", nil)
if err != nil {
return nil, err
}
req = req.WithContext(ctx)
res, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode < 200 || res.StatusCode > 299 {
return nil, fmt.Errorf("bogus status: got %v", res.Status)
}
t := new(T)
if err := json.NewDecoder(res.Body).Decode(t); err != nil {
return nil, err
}
return t, nil
}
标签:return,nil,err,res,req,yaml,json,go From: https://www.cnblogs.com/cheyunhua/p/17465267.html