从标准input读取大量的数据

你如何从Golang的标准input中读取大量数据? 我所有的读取目前停止在4095字节。 我已经尝试了很多东西,但我目前的代码如下所示:

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 } 

我也试过使用扫描仪,但无法弄清楚如何退出

 for scanner.Scan() { data = append(data, scanner.Bytes()...) } 

当一个换行符发生(即当用户按下返回)。

我也尝试了ReadBytes('\ n'),但是即使是停止在4095字节。 缓冲区的大小(这只是一个丑陋的黑客)的短缺,我不知道该怎么做在这一点上。

如果你看看Go来源,你会看到它使用默认的缓冲区大小:

 func NewReader(rd io.Reader) *Reader { return NewReaderSize(rd, defaultBufSize) } 

所以你可以在你的代码中使用:

 var stdinReader = bufio.NewReaderSize(os.Stdin, 10000) 

PS Go是开源的,所以你可以从里面看内部知识。