If you go through the architecture you specified, you have a good chance of exiting CPU / Mem / etc, because you are going to create an arbitrary number of workers. I suggest, instead, go with an architecture that allows throttling through channels. For instance:
In the main process, submitting files to the channel:
for _, file := range folder { fileChan <- file }
then in another goroutine break the files into lines and transfer them to the channel:
for { select{ case file := <-fileChan for _, line := range file { lineChan <- line } } }
then in the third larynx pop out the lines and do what you will with them:
for { select{ case line := <-lineChan:
The main advantage of this is that you can create as many or several running procedures that your system can process and transmit to them all the same channels, and depending on which procedure is launched on the channel, it will process it first, so that you can throttle the amount of resources you use.
Here is a working example: http://play.golang.org/p/-Qjd0sTtyP
source share