Reading a file concurrently

前端 未结 2 1055
轻奢々
轻奢々 2021-01-31 05:36

The reading part isn\'t concurrent but the processing is. I phrased the title this way because I\'m most likely to search for this problem again using that phrase. :)

2条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-31 06:07

    Edit: The answer by @tomasz above is the correct one. Please disregard this answer.

    You need to do two things:

    1. use buffered chan's so that sending doesn't block
    2. close the results chan so that receiving doesn't block.

    The use of buffered channels is essential because unbuffered channels need a receive for each send, which is causing the deadlock you're hitting.

    If you fix that, you'll run into a deadlock when you try to receive the results, because results hasn't been closed.

    Here's the fixed playground: http://play.golang.org/p/DtS8Matgi5

提交回复
热议问题