golang 并发处理
背景
需要并发的处理的任务
查阅
使用 waitgroup chan
代码实现
package main
import (
"fmt"
"sync"
)
func main() {
var l []int
receive := func(w chan int, wg *sync.WaitGroup) {
for data := range w {
l = append(l, data)
wg.Done()
}
close(w)
}
send := func(index int, w chan int) {
w <- index
}
wg := new(sync.WaitGroup)
ws := make(chan int)
go receive(ws, wg)
for i := 0; i < 100; i++ {
wg.Add(1)
go send(i, ws)
}
wg.Wait()
fmt.Print(len(l), "\t")
}
本文来自博客园,作者:vx_guanchaoguo0,转载请注明原文链接:https://www.cnblogs.com/guanchaoguo/p/16139896.html