How do you read lots of data from stdin in Golang? All of my reads currently stop at 4095 bytes. I've tried lots of things but my current code looks like:
var stdinReader = bufio.NewReader(os.stdin)
// Input reads from stdin while echoing back.
func Input(prompt string) []byte {
var data []byte
// Output prompt.
fmt.Print(prompt)
// Read until newline.
for {
bytes, isPrefix, _ := stdinReader.ReadLine()
data = append(data, bytes...)
if !isPrefix {
break
}
}
// Everything went well. Return the data.
return data
}
I've also tried using a scanner but couldn't figure out how to exit
for scanner.Scan() {
data = append(data, scanner.Bytes()...)
}
when a newline occurred (i.e. when the user pressed return).
I also tried ReadBytes(' ') but even that stopped at 4095 bytes. Short of increasing the size of the buffer (which is just an ugly hack) I'm not sure what to do at this point.
If you'll look at Go sources, you will see that it uses default buffer size:
func NewReader(rd io.Reader) *Reader {
return NewReaderSize(rd, defaultBufSize)
}
So you can use in your code as:
var stdinReader = bufio.NewReaderSize(os.Stdin, 10000)
P.S. Go is open source, so you can learn a lot just from looking inside internals.
I believe this has to do with terminals having a fixed sized buffer for stdin input. I'm not sure that there's any way around this other than piping data in or reading it from a file or a network location.