aboutsummaryrefslogtreecommitdiff
path: root/example_test.go
diff options
context:
space:
mode:
authorBen Johnson <benbjohnson@yahoo.com>2014-02-15 14:54:45 -0700
committerBen Johnson <benbjohnson@yahoo.com>2014-02-15 14:54:45 -0700
commit56b825fb56a29da6e460475fcd5a98feb795f194 (patch)
tree5ea69a648a4fb259e8f8b9c855fc7eba7e276894 /example_test.go
parentMerge pull request #31 from benbjohnson/sequence (diff)
downloaddedo-56b825fb56a29da6e460475fcd5a98feb795f194.tar.gz
dedo-56b825fb56a29da6e460475fcd5a98feb795f194.tar.xz
Add transactional blocks.
Diffstat (limited to 'example_test.go')
-rw-r--r--example_test.go27
1 files changed, 27 insertions, 0 deletions
diff --git a/example_test.go b/example_test.go
index 892807e..655e283 100644
--- a/example_test.go
+++ b/example_test.go
@@ -60,6 +60,33 @@ func ExampleDB_Delete() {
// The value of 'foo' is now: nil
}
+func ExampleDB_Do() {
+ // Open the database.
+ var db DB
+ db.Open("/tmp/bolt/db_do.db", 0666)
+ defer db.Close()
+
+ // 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 our transactional block didn't return an error then our data is saved.
+ if err == nil {
+ value, _ := db.Get("widgets", []byte("foo"))
+ fmt.Printf("The value of 'foo' is: %s\n", string(value))
+ }
+
+ // Output:
+ // The value of 'foo' is: bar
+}
+
func ExampleRWTransaction() {
// Open the database.
var db DB