Zum Inhalt springen

Stream Not Closed (ARCH5081)

Dieser Inhalt ist noch nicht in deiner Sprache verfügbar.

Key go.performance.stream-not-closed · Severity error · Category performance

A stream opened without being closed - such as an HTTP response body - leaks the connection and file descriptor. Close the body with defer resp.Body.Close().

func fetch(url string) ([]byte, error) {
resp, err := http.Get(url)
if err != nil {
return nil, err
}
return io.ReadAll(resp.Body)
}
func fetch(url string) ([]byte, error) {
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
return io.ReadAll(resp.Body)
}

Guarantee the stream is closed with defer body.Close() after opening it.