In Go, resource management is critical because unlike memory (which is garbage collected), external resources like file descriptors and network sockets must be released explicitly. Failure to do so leads to leaks that can crash your application.
Here is a checklist of the most common things you need to close, typically using the defer keyword.
This is the most common source of leaks in Go. When you make an HTTP request using the net/http client, you must close the response body, even if you don't read it. If you don't, the underlying network connection cannot be reused (keep-alive) or released.
- What to close:
http.Response.Body - Pattern: