boltdb/bolt — pull request #32 by benbjohnson (2014-02-15 22:00:55+00:00)
Title: Transactional Blocks

## Overview

This pull request includes support for Transactional Blocks. This allows users to execute a transaction within a function without having to worry about deferring the commit/rollback. Any error returned from the function or that returns from the transaction's `Commit()` is returned from the block.

The transactional block is executed using the following method:

```go
func (db *DB) Do(fn func(*RWTransaction) error) error
```

A read-only transactional block may be added in the future. Not sure what to call it yet.

## Example

```go
func myFunc() error {
...

// Execute several commands within a write transaction.
err := db.Do(func(t *RWTransaction) error {
if err := t.CreateBucket("widgets"); err != nil {
return err
}
if err := t.Put("widgets", []byte("foo"), []byte("bar")); err != nil {
return err
}
return nil
})

if err != nil {
return err
}

...
}
```

/cc @snormore