Hacker News new | ask | show | jobs
by timewasted 4817 days ago
Getting files closed on errors should look something like:

    file, err := os.Open(filename)
    if err != nil {
        // handle the error however you see fit
    }
    defer file.Close()
    // continue working, knowing that file.Close() will be called when the surrounding function returns
See: http://golang.org/doc/effective_go.html#defer
1 comments

Ah! Thanks! I had seen panic/recover but hadn't caught "defer". That will make my code much better.
"defer" is 50% of the reason I use Go. Open some resource, then defer close it the next line and move on. No more giant, nested try/finally blocks.
In addition to defer, Go actually does have goto.