Skip to content

Instantly share code, notes, and snippets.

@gregworley
Created August 25, 2011 17:32
Show Gist options
  • Save gregworley/1171231 to your computer and use it in GitHub Desktop.
Save gregworley/1171231 to your computer and use it in GitHub Desktop.
example of func(*iter)Next from http://goneat.org/pkg/launchpad.net/mgo/#Iter.Next
//working example of func(*iter)Next from: http://goneat.org/pkg/launchpad.net/mgo/#Iter.Next
package main
import (
"fmt"
"launchpad.net/mgo"
)
type Person struct {
Name string
Phone string
}
func main() {
session, err := mgo.Mongo("mongotest")
if err != nil {
panic(err)
}
defer session.Close()
session.SetMode(mgo.Monotonic, true)
c := session.DB("test").C("people")
err = c.Insert(&Person{"Ale", "+55 53 8116 9639"},
&Person{"Cla", "+55 53 8402 8510"})
if err != nil {
panic(err)
}
var result *Person
iter, err := c.Find(nil).Iter()
if err != nil {
panic(err)
}
for {
err = iter.Next(&result)
if err != nil {
break
}
fmt.Println(result.Name, " Phone:", result.Phone)
}
if err != mgo.NotFound {
panic(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment