Stream Not Closed (ARCH5081)
Ce contenu n’est pas encore disponible dans votre langue.
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().
Incorrect
Section titled “Incorrect”func fetch(url string) ([]byte, error) { resp, err := http.Get(url) if err != nil { return nil, err } return io.ReadAll(resp.Body)}Correct
Section titled “Correct”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.