why does bufionewreader not work when the input is a redirect?

Your use of bufio.NewReader is wrong. You create a new buffered reader each time through the i loop. The reader may read ahead if input is available, and when you don’t re-use the reader that buffered input is unused.

Simply move rd := bufio.NewReader(os.Stdin) outside of the loop to fix the problem.

Your code works when you read from the console via stdin because you can’t type fast enough for the reader to try to buffer input.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top