Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read large amount of data from standard input

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('\n') 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.

like image 529
Awn Avatar asked Nov 08 '22 00:11

Awn


1 Answers

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.

like image 65
Alexander R. Avatar answered Nov 14 '22 21:11

Alexander R.