On 21 August 2012 07:40, Brian Slesinsky <
bsles...@gmail.com> wrote:
> Let's say we want to write cat(1). Of course we don't really need to use
> concurrency at all; the program could just loop over the filenames and copy
> each one.
>
> However, suppose that a few of the files in the middle of the list are on
> very slow network filesystems. Then we probably don't want to open the files
> sequentially; instead we could start some goroutines and open them all in
> parallel. Each reader goroutine could open the file, read the data, and send
> it somehow to a writer goroutine that writes the file contents in the proper
> order. But there should be some back-pressure so we don't run out of memory.
If your use case really is like cat, and the output is written to a
io.Writer in an specific order, you don't need to send the data.
Instead, read to a buffer in each goroutine and pass the io.Writer to
the goroutine working on the next file when you are done.
If you want to store the data for later, write it to a buffer. In the
case you know the size of the files, allocate a big buffer for the
result in advance and pass slices of this big buffer to the different
goroutines, to use as less memory as possible.
--
- yiyus || JGL .