I have the following code running in a 64-bit linux OS in a 4Gb machine:
package main
import (
"fmt"
"math"
)
func main() {
r := make([]bool, math.MaxInt32)
fmt.Println("Size: ", len(r))
}
When I run this I get:
Size: 2147483647
If I change the math.MaxInt32
for math.MaxUint32
I get:
fatal error: runtime: out of memory
With slice size of math.MaxUint32
I ran out of memory, I was expecting that, but when I try using math.MaxInt64
I get:
panic: runtime error: makeslice: len out of range
So aparently I cannot create a slice with a size of math.MaxInt64
, which bring us to my question: If the memory is not an issue, what's the biggest slice I cant create in Go?
I remember that, in Java, raw array indexes are managed with the type int
, so the maximum size of a raw array is the max value of an int
, if you try to do it with long
it will raise an exception (as far as I remember), is it the same with Go? are slice index in Go bound to one specific type?
EDIT:
I ran the test using struct{}
instead of bool
and allocating math.MaxInt64
elements. Everything went as expected, and prints:
Size: 9223372036854775807
So, another question, why there are two different error messages when it seems that the error is the same (not enough memory)?
What are the conditions for each error to pop out?