Go 匿名函数的参数错误-不是类型
有人能向我澄清一下,为什么golang抱怨goroutine中的代码“工作不是一种类型”吗Go 匿名函数的参数错误-不是类型,go,Go,有人能向我澄清一下,为什么golang抱怨goroutine中的代码“工作不是一种类型”吗 type job func(in, out chan interface{}) func Execute(jobs ...job) { in := make(chan interface{}, 100) out := make(chan interface{}, 100) wg := &sync.WaitGroup{} for _, job := range j
type job func(in, out chan interface{})
func Execute(jobs ...job) {
in := make(chan interface{}, 100)
out := make(chan interface{}, 100)
wg := &sync.WaitGroup{}
for _, job := range jobs {
wg.Add(1)
go func(j job, waiter *sync.WaitGroup, in, out chan interface{}) {
defer waiter.Done()
defer close(out)
j(in, out)
}(job, wg, in, out)
in = out
out = make(chan interface{}, 100)
}
wg.Wait()
}
如果我创建单独的函数,或在循环外执行goroutine-所有编译都很好,这是因为for循环:
for _, job := range jobs {
// In here, job is the loop variable
}
循环变量
job
正在隐藏类型名job
。如果将循环变量重命名为其他变量,它应该可以工作。对不起,这是我的错误类型作业,它隐藏在循环变量的名称中!你能给源代码加一些解释吗
type job func(in, out chan interface{})
.
.
.
for _, jb := range jobs {
.
.
.
}(jb, wg, in, out)