此代碼將永遠不會退出,直到它爲len(queues)
次做處理。它不是一個併發代碼 - 全部在主體中 - 並且沒有告訴代碼停止的信號。問題就在這裏:
case "run":
// Installing the service
installed, err := service.Install()
logError(err, installed)
// Starting the service
started, err := service.Start()
logError(err, started)
if err == nil {
// Creating a goroutine and executing the queue's processes in parallel
for i := 0; i < len(queues); i++ {
go startProcessing(queues[i])
time.Sleep(time.Second) // Waiting for other functions to execute
}
select {} // To prevent the goroutine from exiting the main func
}
fmt.Println(started)
因爲它可以看出,select{}
行會坐在那裏跑,直到永遠! :)最好是有移動這種情況下的條款,到他們自己夠程,並有一個退出信號是這樣的:
select {
case <-quit:
return
}
雖然這不是處理開始在Go應用程序/停止最徹底的方法;它只是表明了問題。