package main
import (
"fmt"
"sync"
)
type worker struct {
id int
c chan int
wg *sync.WaitGroup
}
func createWorker(id int, wg *sync.WaitGroup) worker {
c := make(chan int)
return worker{id, c, wg}
}
func (worker *worker) doWork() {
for v := range worker.c {
fmt.Printf("Worker %d do work: %c\n", worker.id, v)
worker.wg.Done()
}
}
func main() {
var wg sync.WaitGroup
var workers [10] worker
//init worker
for i := 0; i < 10; i++ {
workers[i] = createWorker(i, &wg)
}
//Do work
wg.Add(10)
//for i := 0; i < 10; i++ {
// go workers[i].doWork()
//}
//一旦换成range的方式遍历,就会报deadlock的错,为什么呀?
for _, worker := range workers{
go worker.doWork()
}
//Send data
for i := 0; i < 10; i++ {
workers[i].c <- 'a' + i
}
wg.Wait()
}
一旦换成range的方式遍历,就会报deadlock的错,为什么呀?