I saw the example 
<https://godoc.org/golang.org/x/sync/errgroup#example-Group--Parallel> of 
errgroup in godoc, and it makes me confused that it simply assigns the 
result to global results instead of using channels in each search routines. 
Heres the code:

Google := func(ctx context.Context, query string) ([]Result, error) {
    g, ctx := errgroup.WithContext(ctx)

    searches := []Search{Web, Image, Video}
    results := make([]Result, len(searches))
    for i, search := range searches {
        i, search := i, search // 
https://golang.org/doc/faq#closures_and_goroutines
        g.Go(func() error {
            result, err := search(ctx, query)
            if err == nil {
                results[i] = result
            }
            return err
        })
    }
    if err := g.Wait(); err != nil {
        return nil, err
    }
    return results, nil
}


I'm wondering is there any mechanism or implied rules that makes the *write 
*in sub goroutines a happens before of *read *in main goroutine? Thanks for 
your time.

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to