📜  golang max limit workers goroutines - 任何代码示例

📅  最后修改于: 2022-03-11 14:56:52.693000             🧑  作者: Mango

代码示例1
package main

import (
    "fmt"
    "sync"
)

var wg sync.WaitGroup

func main() {
    const max = 10
    queue := make(chan int, max)
    for i := 0; i < max; i++ {
        wg.Add(1)
        queue <- 1
        go worker(queue)
    }
    close(queue)
    wg.Wait()
    fmt.Println("Done")
}
func worker(queue chan int) {
    for job := range queue {
        fmt.Print(job, " ") // a job
    }
    <-queue
    defer wg.Done()
}