Add bucket retrieve example to README

Signed-off-by: Cenk Alti <cenkalti@gmail.com>
pull/500/head
Cenk Alti 2023-05-16 16:04:31 -04:00
parent 160cd29ec3
commit 6ec43f8da3
No known key found for this signature in database
GPG Key ID: 30BB97EF64A82993
1 changed files with 11 additions and 0 deletions

View File

@ -296,6 +296,17 @@ db.Update(func(tx *bolt.Tx) error {
})
```
You can retrieve an existing bucket using the `Tx.Bucket()` function:
```go
db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("MyBucket"))
if b == nil {
return fmt.Errorf("bucket does not exist")
}
return nil
})
```
You can also create a bucket only if it doesn't exist by using the
`Tx.CreateBucketIfNotExists()` function. It's a common pattern to call this
function for all your top-level buckets after you open your database so you can