func (b *Bucket) ForEach(fn func(k, v []byte) error) error {
	if b.tx.db == nil {
		return ErrTxClosed
	}
	c := b.Cursor()
	for k, v := c.First(); k != nil; k, v = c.Next() {
		if err := fn(k, v); err != nil {
			return err
		}
	}
	return nil
}
ForEach executes a function for each key/value pair in a bucket.
If the provided function returns an error then the iteration is stopped and
the error is returned to the caller.
package bolt
import (
	"bytes"
	"fmt"
	"unsafe"
)
const (
	MaxKeySize = 32768
	MaxValueSize = 4294967295
)
const (
	maxUint = ^uint(0)
	minUint = 0
	maxInt  = int(^uint(0) >> 1)
	minInt  = -maxInt - 1
)
const bucketHeaderSize = int(unsafe.Sizeof(bucket{}))
const (
	minFillPercent = 0.1
	maxFillPercent = 1.0
)
const DefaultFillPercent = 0.5
type Bucket struct {
	*bucket
	tx       *Tx                 
	buckets  map[string]*Bucket  
	page     *page               
	rootNode *node               
	nodes    map[pgid]*node      
	FillPercent float64
}
type bucket struct {
	root     pgid    
	sequence uint64  
}
func newBucket(tx *Tx) Bucket {
	var b = Bucket{tx: tx, FillPercent: DefaultFillPercent}
	if tx.writable {
		b.buckets = make(map[string]*Bucket)
		b.nodes = make(map[pgid]*node)
	}
	return b
}
func (b *Bucket) Tx() *Tx {
	return b.tx
}
func (b *Bucket) Root() pgid {
	return b.root
}
func (b *Bucket) Writable() bool {
	return b.tx.writable
}
func (b *Bucket) Cursor() *Cursor {
	b.tx.stats.CursorCount++
	return &Cursor{
		bucket: b,
		stack:  make([]elemRef, 0),
	}
}
func (b *Bucket) Bucket(name []byte) *Bucket {
	if b.buckets != nil {
		if child := b.buckets[string(name)]; child != nil {
			return child
		}
	}
	c := b.Cursor()
	k, v, flags := c.seek(name)
	if !bytes.Equal(name, k) || (flags&bucketLeafFlag) == 0 {
		return nil
	}
	var child = b.openBucket(v)
	if b.buckets != nil {
		b.buckets[string(name)] = child
	}
	return child
}
func (b *Bucket) openBucket(value []byte) *Bucket {
	var child = newBucket(b.tx)
	if b.tx.writable {
		child.bucket = &bucket{}
		*child.bucket = *(*bucket)(unsafe.Pointer(&value[0]))
	} else {
		child.bucket = (*bucket)(unsafe.Pointer(&value[0]))
	}
	if child.root == 0 {
		child.page = (*page)(unsafe.Pointer(&value[bucketHeaderSize]))
	}
	return &child
}
func (b *Bucket) CreateBucket(key []byte) (*Bucket, error) {
	if b.tx.db == nil {
		return nil, ErrTxClosed
	} else if !b.tx.writable {
		return nil, ErrTxNotWritable
	} else if len(key) == 0 {
		return nil, ErrBucketNameRequired
	}
	c := b.Cursor()
	k, _, flags := c.seek(key)
	if bytes.Equal(key, k) {
		if (flags & bucketLeafFlag) != 0 {
			return nil, ErrBucketExists
		} else {
			return nil, ErrIncompatibleValue
		}
	}
	var bucket = Bucket{
		bucket:      &bucket{},
		rootNode:    &node{isLeaf: true},
		FillPercent: DefaultFillPercent,
	}
	var value = bucket.write()
	key = cloneBytes(key)
	c.node().put(key, key, value, 0, bucketLeafFlag)
	b.page = nil
	return b.Bucket(key), nil
}
func (b *Bucket) CreateBucketIfNotExists(key []byte) (*Bucket, error) {
	child, err := b.CreateBucket(key)
	if err == ErrBucketExists {
		return b.Bucket(key), nil
	} else if err != nil {
		return nil, err
	}
	return child, nil
}
func (b *Bucket) DeleteBucket(key []byte) error {
	if b.tx.db == nil {
		return ErrTxClosed
	} else if !b.Writable() {
		return ErrTxNotWritable
	}
	c := b.Cursor()
	k, _, flags := c.seek(key)
	if !bytes.Equal(key, k) {
		return ErrBucketNotFound
	} else if (flags & bucketLeafFlag) == 0 {
		return ErrIncompatibleValue
	}
	child := b.Bucket(key)
	err := child.ForEach(func(k, v []byte) error {
		if v == nil {
			if err := child.DeleteBucket(k); err != nil {
				return fmt.Errorf("delete bucket: %s", err)
			}
		}
		return nil
	})
	if err != nil {
		return err
	}
	delete(b.buckets, string(key))
	child.nodes = nil
	child.rootNode = nil
	child.free()
	c.node().del(key)
	return nil
}
func (b *Bucket) Get(key []byte) []byte {
	k, v, flags := b.Cursor().seek(key)
	if (flags & bucketLeafFlag) != 0 {
		return nil
	}
	if !bytes.Equal(key, k) {
		return nil
	}
	return v
}
func (b *Bucket) Put(key []byte, value []byte) error {
	if b.tx.db == nil {
		return ErrTxClosed
	} else if !b.Writable() {
		return ErrTxNotWritable
	} else if len(key) == 0 {
		return ErrKeyRequired
	} else if len(key) > MaxKeySize {
		return ErrKeyTooLarge
	} else if int64(len(value)) > MaxValueSize {
		return ErrValueTooLarge
	}
	c := b.Cursor()
	k, _, flags := c.seek(key)
	if bytes.Equal(key, k) && (flags&bucketLeafFlag) != 0 {
		return ErrIncompatibleValue
	}
	key = cloneBytes(key)
	c.node().put(key, key, value, 0, 0)
	return nil
}
func (b *Bucket) Delete(key []byte) error {
	if b.tx.db == nil {
		return ErrTxClosed
	} else if !b.Writable() {
		return ErrTxNotWritable
	}
	c := b.Cursor()
	_, _, flags := c.seek(key)
	if (flags & bucketLeafFlag) != 0 {
		return ErrIncompatibleValue
	}
	c.node().del(key)
	return nil
}
func (b *Bucket) NextSequence() (uint64, error) {
	if b.tx.db == nil {
		return 0, ErrTxClosed
	} else if !b.Writable() {
		return 0, ErrTxNotWritable
	}
	if b.rootNode == nil {
		_ = b.node(b.root, nil)
	}
	b.bucket.sequence++
	return b.bucket.sequence, nil
}