Thank you for helping me.The other question is: if I only change fetch function,how to make the same result??
在 2017年11月13日星期一 UTC+8下午9:08:00,Ayan George写道: > > > > On 11/13/2017 04:10 AM, [email protected] <javascript:> wrote: > > > for range os.Args[1:] { > > fmt.Println(<-ch) > > } > > fmt.Printf("%.2fs elasped\n", time.Since(start).Seconds()) > > } > > If I understand correctly, you want fetch() to get each URL twice but > you only read from the 'ch' channel once per URL. > > So if I passed it 3 urls, we should expect to receive at least 6 > responses back on ch but you only read from it 3 times. > > If that loop above was something more like (and this is a kludge): > > for range append(os.Args[1:], os.Args[1:]...) { > fmt.Println(<-ch) > } > > or maybe better: > > for i := 0; i < len(os.Args[1:]) * 2; i++ { > fmt.Println(<-ch) > } > > The problem is that you return from fetch() early if there is an error > so you might end up blocking on <-ch forever because the number of sends > to that channel will not match the number of receives. > > If I were you, I'd consider using a sync.WaitGroup to wait for all of > the fetch() calls to complete and store the results in a buffered channel. > > Once all of the fetch() calls are done, you can close the channel and > range over it like this: > > https://play.golang.org/p/iCNyvpE4Y1 > > > -- 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.
