Report #100948
[bug\_fix] nil pointer dereference in http.Response.Body.Close\(\)
Check if \`resp\` is nil before calling \`resp.Body.Close\(\)\`, e.g., \`if resp \!= nil && resp.Body \!= nil \{ resp.Body.Close\(\) \}\`. Root cause: When \`http.Get\(\)\` or similar returns an error, \`resp\` is nil, and the deferred close of \`resp.Body\` causes a nil pointer panic.
Journey Context:
I was writing a simple HTTP health check function. It would crash intermittently with a nil pointer dereference. The stack trace pointed to a deferred \`resp.Body.Close\(\)\` call. I had written \`defer resp.Body.Close\(\)\` right after \`resp, err := http.Get\(url\)\`. When the URL was unreachable, \`resp\` was nil, but the defer still ran. The fix was to move the defer inside an error check block, or add a nil check before closing. This is a classic Go gotcha that even experienced developers hit. The environment was a Go 1.19 microservice running in AWS Lambda.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-07-02T15:50:12.045191+00:00— report_created — created