Reputation: 2287
I want to input 200000 space separated strings of arbitary numbers. When taking the input using bufio.Reader
it only take a few of them. Here is the code:
package main
import (
"bufio"
"fmt"
"io"
"os"
"strings"
)
func main() {
reader := bufio.NewReaderSize(os.Stdin, 1024*1024)
scoresTemp := strings.Split(readLine(reader), " ")
fmt.Println(scoresTemp)
fmt.Println("---")
fmt.Println(len(scoresTemp))
}
func readLine(reader *bufio.Reader) string {
str, _, err := reader.ReadLine()
if err == io.EOF {
return ""
}
return strings.TrimRight(string(str), "\r\n")
}
The slice length should be 200000, but it only take 410 items. If I increase the size of reader, it would be the same. What is the max size of slice of strings in Go?, How to work on it?
Upvotes: 0
Views: 3492
Reputation: 3795
Your code doesn't iterate on the input very well. The problem isn't with slices.
Try the following code if it does what you want.
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
inputScanner := bufio.NewScanner(os.Stdin)
inputScanner.Split(bufio.ScanWords)
scoresTemp := make([]string, 0, 200000)
for inputScanner.Scan() {
scoresTemp = append(scoresTemp, inputScanner.Text())
}
fmt.Println(scoresTemp)
fmt.Println("---")
fmt.Println(len(scoresTemp))
}
For the explanation:
bufio.Scanner
helps "scanning" a certain input and splitting it in whichever way you like. By default it splits be new lines. Which brings us to number 2.bufio.ScanWords
is a function that splits bytes on spaces (including new-lines). This defines the behaviour of inputScanner
. Upvotes: 1
Reputation: 3970
I believe you have an issue with your input rather than on your Go code. I've tried your code on my local machine and got this result:
$ for((i=0;i<200000;i++)) do echo -n "x "; done | go run main.go
...
---
200001
Upvotes: 1