mfathirirhas
mfathirirhas

Reputation: 2287

Max size of slice of string in Go

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

Answers (2)

Seaskyways
Seaskyways

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:

  1. 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.
  2. bufio.ScanWords is a function that splits bytes on spaces (including new-lines). This defines the behaviour of inputScanner.
  3. Next comes the slice of strings where we store our data. It's initialized with 0 elements, and capacity of 200,000 strings. This optimizes allocation times.
  4. PRINT !!

Upvotes: 1

ssemilla
ssemilla

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

Related Questions