Вопрос

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 ?

Это было полезно?

Решение

You're right about the specs, they define that when main returns, all goroutines are killed:

When the function main returns, the program exits. It does not wait for other (non-main) goroutines to complete.

There are two options.

  • Synchronize with your goroutines at the end of main. A "done" chanel can be used or a sync.WaitGroup.

  • Where appropriate a blocking empty select statement could be used:

    select{} // Non busy block forever

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top