此代码将永远不会退出,直到它进行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{}条线将坐在那里并永远运行!:) 最好将这个 case 子句移到他们自己的 goroutine 中,并在那里有一个退出信号,如下所示:
select {
case <-quit:
return
}
尽管这不是在 Go 应用程序中处理启动/停止的最简洁方式;它只是显示了问题。