Hacker News new | ask | show | jobs
by barsonme 3104 days ago
It's not terribly uncommon for larger functions to do something like

    func someSQLStuff() {
        tx, err := createTx()     
   
        defer func() {
            if err != nil {
                tx.Rollback()
                log(err)
            } else {
                tx.Commit()
            }
         }()

         rows, err = tx.QueryContext( ... )

         // more SQL
    }
Basically, function-scoped cleanup. Like closing opened files.
1 comments

But that defer is already lexically at the function scope, so block-scoped defer would do the same thing.
Ah, sorry, you meant function-scoped vs block-scoped not just in general. Yeah, agreed.