Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use Get() and the ResultIterator? #34

Closed
bobvanluijt opened this issue Jun 18, 2017 · 3 comments
Closed

How to use Get() and the ResultIterator? #34

bobvanluijt opened this issue Jun 18, 2017 · 3 comments

Comments

@bobvanluijt
Copy link

Hi all,

I'm kinda stuck on using the Get() function.

My result should return three DB entries which I try to retrieve like this:
result, err := txn.Get("foobar", "RefType", refType)

How do I go over the results one by one?

Thanks!

@slackpad
Copy link
Contributor

Hi @bobvanluijt you call .Next() on it to iterate through the items - see https://github.com/hashicorp/go-memdb/blob/master/txn_test.go#L451-L467 for an example. You have to call .Next() one time to get the first item, and it will return nil when the iterator is exhausted, so you can also do:

result, err := txn.Get("foobar", "RefType", refType)
if err != nil {
    return err
}

for item := result.Next(); item != nil; item = result.Next() {
    // handle each item
}

Hope that helps!

@bobvanluijt
Copy link
Author

Ooo, thanks for that!

@fallais
Copy link

fallais commented Feb 8, 2019

You should put it in the README, very useful ! I was also stuck !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants