From this file, I do not understand why the function startWorker is written like the following.:
func (p *WorkerPool) dispatch() {
for i := 0; i < p.maxWorkers; i++ {
p.taskQueue[i] = make(chan func())
go startWorker(p.taskQueue[i])
}
}
func startWorker(taskChan chan func()) {
go func() {
var task func()
var ok bool
for {
task, ok = <-taskChan
if !ok {
break
}
// Execute the task.
task()
}
}()
}
If I were the developer, I will write this function like this:
func startWorker(taskChan chan func()) {
var task func()
var ok bool
for {
task, ok = <-taskChan
if !ok {
return
}
// Execute the task.
task()
}
}