I'm new to Go and I'm trying to write a simple program that iterates over all users in the MongoDB database and for each user iterates over all of his posts, using the 'mgo' package.
package main
import (
"fmt"
"labix.org/v2/mgo"
"labix.org/v2/mgo/bson"
)
type User struct {
Id string
Email string
}
type Post struct {
Id string
Description string
}
func handleUser(db *mgo.Database, user *User) {
fmt.Println("ID: ", user.Id, " EMAIL: ", user.Email)
result := Post{}
iter := db.C("posts").Find(bson.M{"user_id": user.Id}).Iter()
for iter.Next(&result) {
fmt.Println("POST ID: ", result.Id, " POST DESCRIPTION: ", result.Description)
}
}
func main() {
session, err := mgo.Dial("localhost")
if err != nil {
panic(err)
}
defer session.Close()
db := session.DB("mydb")
result := User{}
iter := db.C("users").Find(nil).Iter()
for iter.Next(&result) {
handleUser(db, &result)
}
}
this is working just fine,
but if I try to change the call to handleUser(db, &result)
to go handleUser(db, &result)
the second query inside handleUser doesn't do anything.
I suspect that the session is already closed because 'main' already finished, am I correct? if so, what is the way to handle such scenario ?