DEV Community

Cover image for Defer in Golang

Defer in Golang

Amit Tiwary on January 20, 2024

In my last blog I used defer function and use resolve inside it. defer is one of the important feature of golang. defer statement execute just befo...
Collapse
 
syxaxis profile image
George Johnson

I will jonkingly add, just be self aware on "defer". In my early Go journey I got hooked on defers to make the code sharp and efficient on resources, I used them all the time, but I once spent 3 hours debugging why my DB and stream handles were closed when i needed them and realised I'd gone "defer blind" it was closing my open handles for me but I couldn't see my defers! Ha ha!!

Collapse
 
amitiwary999 profile image
Amit Tiwary

Thanks for sharing your experience. It will help everyone including me.