Golang 通过 管道控制并发数量
package main import ( "fmt" "math/rand" "sync" "time" ) const concurrency = 20 func main() { rateChannel := make(chan int, concurrency)
// 别忘记 关闭管道 defer close(rateChannel) // channel 控制并发数量 go func() { for { rateChannel <- 1 time.Sleep(time.Duration(1000)) } }() var wg sync.WaitGroup for{ <- rateChannel wg.Add(1) go func(wg *sync.WaitGroup) { rand.Seed(1) i := rand.Int() fmt.Println(i) }(&wg) } }
邮箱: 1090055252@qq.com