使用结构时,Golang会收到未定义的类型错误

时间:2017-05-18 18:44:26

标签: go

我正在使用来自http://marcio.io/2015/07/handling-1-million-requests-per-minute-with-golang/的线程池示例,我正在关注该教程但是我在编译时遇到此错误 d.pool undefined(类型* Dispatcher没有字段或方法池)< / strong>这是代码

   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()
    }

此代码发生错误

worker:= NewWorker(d.pool)

任何解决方案或建议都会很棒,因为我是新手,但我正在尝试实现一个线程池

1 个答案:

答案 0 :(得分:0)

错误意味着它所说的内容,您的Dispatcher类型没有名为pool的字段或方法。但是,它确实有一个名为WorkerPool的字段,这是我猜你要引用的字段。