Go启动10个goroutine处理任务的例子 2020-05-22 Go语言 Go, Golang 0 评论 字数统计: 109(字) Go语言启动10个goroutine处理任务的例子 1234567891011121314151617181920212223242526package mainimport ( "fmt" "time")func main() { channels := make([]chan int, 10) for i := 0; i < 10; i++ { channels[i] = make(chan int) go process(channels[i]) } for i, ch := range channels { <-ch fmt.Println("routine:", i, "quit") }}func process(ch chan int) { // 模拟处理任务 time.Sleep(time.Second) ch <- 1} 输出 12345678910routine: 0 quitroutine: 1 quitroutine: 2 quitroutine: 3 quitroutine: 4 quitroutine: 5 quitroutine: 6 quitroutine: 7 quitroutine: 8 quitroutine: 9 quit