What's the recommended way to allow more than 1024 characters when reading from standard input in Go?
For example, this code using bufio.Scanner
has a maximum input length of 1024.
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
input := scanner.Text()
fmt.Println(input)
}
Update after some suggested answers... Sorry guys - I must still be doing something wrong, or misstated the question. I tried both suggestions and still see the same issue. Below is an updated version of the code. The symptom is that the scanner won't accept input after the 1024th character. e.g. Try to run this then paste in a string that's 1025 characters long, and it'll stop accepting input after character 1024.
package main
import (
"bufio"
"bytes"
"fmt"
"log"
"os"
)
func main() {
var buffer bytes.Buffer
scanner := bufio.NewScanner(os.Stdin)
for {
done := scanner.Scan()
buffer.WriteString(scanner.Text())
if done == true {
if scanner.Err() != nil {
log.Fatal("Error scanning input")
}
break
}
}
fmt.Println(buffer.String())
}