Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Like others have mentioned, RAII-style stuff doesn't work so well with a GC language.

Go does provide a "defer" statement, which cause a function call to execute when the current function returns. This makes it easy to visually co-locate resource acquisition and release, as well as ensuring the release code runs regardless of how the function is exited.

It looks like this:

    f := os.Open("someFile")
    defer f.Close() // will run when current function exits
It's easy and works well (IMO).


Consider applying for YC's Fall 2025 batch! Applications are open till Aug 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: