Skip to content
This repository has been archived by the owner on Mar 9, 2019. It is now read-only.

ForEach() #36

Merged
merged 1 commit into from
Feb 16, 2014
Merged

ForEach() #36

merged 1 commit into from
Feb 16, 2014

Conversation

benbjohnson
Copy link
Member

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():

// 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

benbjohnson added a commit that referenced this pull request Feb 16, 2014
@benbjohnson benbjohnson merged commit f8fd84b into boltdb:master Feb 16, 2014
@benbjohnson benbjohnson deleted the for-each branch February 16, 2014 21:00
heyitsanthony pushed a commit to heyitsanthony/bolt that referenced this pull request Sep 13, 2017
Increase freelist.releaseRange unit test coverage.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant