boltdb/bolt

how to get the value that the current cursor points to

qshuai opened this issue · 3 comments

how to get the value that the current cursor points to

Hi,

Cursors work like allow you to iterate a bucket, you use a combination of the methods First, Last, Next and Previous to return the key and value.

e.g.

c := bucket.Cursor()
k,v := c.First()
while k != nil {
    // do something with key and value
    k, v = c.Next()
}

@lummie Thanks for your reply!

ok, I get. But I encounter the situation that I need get the value in the function by the cursor(this is a input argument).

I think the following is not the best practice:

func test(c *bolt.Cursor) []byte {
	c.Prev()
	_, v := c.Next()
	return v
}

If bolt addd a function Value() to get the value of the current cursor, that is nice!
Is there other choices?

Can you expand on your use case as that seems a little bit of a hack to me.
If you are using the cursor to hold state then maybe the last value retrieved should be held in that state as well?