Use the //bodyclose:handled directive
masterIf you have a function that fully handles every returned *http.Response body (e.g., by closing it or consuming it and then replacing it with a NopCloser), you can annotate the function with //bodyclose:handled.
This tells the analyzer to skip both close and consumption checks for calls to this function, including calls made from other packages. Note that this only works for statically resolved function calls.
// bodyclose:handled
func doRequest(req *http.Request, handle func([]byte) error) (*http.Response, error) {
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if err := handle(body); err != nil {
return nil, err
}
resp.Body = io.NopCloser(bytes.NewReader(body))
return resp, nil
}