boltdb/bolt — pull request #36 by benbjohnson (2014-02-16 21:00:12+00:00)
Title: ForEach()

## Overview

This pull request includes an ease-of-function on the `DB` and `Transaction` for iterating over a bucket.

```
func (db *DB) ForEach(name string, fn func(k, v []byte) error) error
func (t *Transaction) ForEach(name string, fn func(k, v []byte) error) error
```

## Usage

To iterate over all keys and values without worrying about opening and closing your `Transaction`, you can use `ForEach()`:

```go
// Insert data into a bucket.
db.CreateBucket("animals")
db.Put("animals", []byte("dog"), []byte("fun"))
db.Put("animals", []byte("cat"), []byte("lame"))
db.Put("animals", []byte("liger"), []byte("awesome"))

// Iterate over items in sorted key order.
db.ForEach("animals", func(k, v []byte) error {
fmt.Printf("A %s is %s.\n", string(k), string(v))
return nil
})

// Output:
// A cat is lame.
// A dog is fun.
// A liger is awesome.
```

/cc @snormore