我正在研究a blog上有关使用go例程的时间的信息,我看到了下面粘贴的示例(从61行到65行)。但是我没有达到这里使用channel的目的。
似乎他正在迭代通道以检索go-routine中的msg。 但是为什么不直接使用字符串数组呢?
58 func findConcurrent(goroutines int,topic string,docs []string) int {
59 var found int64
60
61 ch := make(chan string,len(docs))
62 for _,doc := range docs {
63 ch <- doc
64 }
65 close(ch)
66
67 var wg sync.WaitGroup
68 wg.Add(goroutines)
69
70 for g := 0; g < goroutines; g++ {
71 go func() {
72 var lFound int64
73 for doc := range ch {
74 items,err := read(doc)
75 if err != nil {
76 continue
77 }
78 for _,item := range items {
79 if strings.Contains(item.Description,topic) {
80 lFound++
81 }
82 }
83 }
84 atomic.AddInt64(&found,lFound)
85 wg.Done()
86 }()
87 }
88
89 wg.Wait()
90
91 return int(found)
92 }