MCPcopy
hub / github.com/etcd-io/bbolt / ExampleCursor

Function ExampleCursor

cursor_test.go:878–930  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

876}
877
878func ExampleCursor() {
879 // Open the database.
880 db, err := bolt.Open(tempfile(), 0600, nil)
881 if err != nil {
882 log.Fatal(err)
883 }
884 defer os.Remove(db.Path())
885
886 // Start a read-write transaction.
887 if err := db.Update(func(tx *bolt.Tx) error {
888 // Create a new bucket.
889 b, err := tx.CreateBucket([]byte("animals"))
890 if err != nil {
891 return err
892 }
893
894 // Insert data into a bucket.
895 if err := b.Put([]byte("dog"), []byte("fun")); err != nil {
896 log.Fatal(err)
897 }
898 if err := b.Put([]byte("cat"), []byte("lame")); err != nil {
899 log.Fatal(err)
900 }
901 if err := b.Put([]byte("liger"), []byte("awesome")); err != nil {
902 log.Fatal(err)
903 }
904
905 // Create a cursor for iteration.
906 c := b.Cursor()
907
908 // Iterate over items in sorted key order. This starts from the
909 // first key/value pair and updates the k/v variables to the
910 // next key/value on each iteration.
911 //
912 // The loop finishes at the end of the cursor when a nil key is returned.
913 for k, v := c.First(); k != nil; k, v = c.Next() {
914 fmt.Printf("A %s is %s.\n", k, v)
915 }
916
917 return nil
918 }); err != nil {
919 log.Fatal(err)
920 }
921
922 if err := db.Close(); err != nil {
923 log.Fatal(err)
924 }
925
926 // Output:
927 // A cat is lame.
928 // A dog is fun.
929 // A liger is awesome.
930}
931
932func ExampleCursor_reverse() {
933 // Open the database.

Callers

nothing calls this directly

Calls 10

tempfileFunction · 0.85
UpdateMethod · 0.80
FirstMethod · 0.80
NextMethod · 0.80
FatalMethod · 0.65
PathMethod · 0.45
CreateBucketMethod · 0.45
PutMethod · 0.45
CursorMethod · 0.45
CloseMethod · 0.45

Tested by

no test coverage detected