go json、yaml解码
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
}