Golang在使用结构时遇到未定义的类型错误
我正在使用中的一个线程池示例,我正在遵循该教程,但是在编译d.pool undefined(type*Dispatcher没有字段或方法池)时出现了此错误。这是代码Golang在使用结构时遇到未定义的类型错误,go,Go,我正在使用中的一个线程池示例,我正在遵循该教程,但是在编译d.pool undefined(type*Dispatcher没有字段或方法池)时出现了此错误。这是代码 type Dispatcher struct { // A pool of workers channels that are registered with the dispatcher WorkerPool chan chan Job } func NewWorker(w
type Dispatcher struct {
// A pool of workers channels that are registered with the dispatcher
WorkerPool chan chan Job
}
func NewWorker(workerPool chan chan Job) Worker {
return Worker{
WorkerPool: workerPool,
JobChannel: make(chan Job),
quit: make(chan bool)}
}
func NewDispatcher(maxWorkers int) *Dispatcher {
pool := make(chan chan Job, maxWorkers)
return &Dispatcher{WorkerPool: pool}
}
func (d *Dispatcher) Run() {
// starting n number of workers
//d.WorkerPool
for i := 0; i < 5; i++ {
worker := NewWorker(d.pool)
worker.Start()
}
go d.dispatch()
}
类型调度程序结构{
//在调度程序中注册的工人通道池
WorkerPool chan chan工作
}
func NewWorker(workerPool chan Job)工人{
返乡工人{
WorkerPool:WorkerPool,
JobChannel:make(chan Job),
退出:使(成波)}
}
func NewDispatcher(maxWorkers int)*调度程序{
池:=制造(chan chan Job,maxWorkers)
返回和调度程序{WorkerPool:pool}
}
func(d*调度程序)运行(){
//工人人数
//d、 工作工具
对于i:=0;i<5;i++{
工人:=新工人(d.pool)
worker.Start()
}
d.调度
}
此代码发生错误
工人:=新工人(d.pool)
任何解决方案或建议都很好,因为我是新手,但我正在尝试实现一个线程池错误的意思正是它所说的,您的
调度程序
类型没有名为池
的字段或方法。但是,它确实有一个名为WorkerPool
的字段,我猜您是想引用它。您是否尝试删除:just do worker=NewWorker(d.pool)Dispatcher
中没有字段pool
。它只有WorkerPool
。是的,我不知道那些家伙的代码如何工作。你的Dispatcher对象没有池,所以d.pool永远不能工作。