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([]boo
According to the docs, The elements can be addressed by integer indices 0 through len(s)-1
. This means the maximum capacity for a slice is the size of the default integer on the target build.
EDIT: From looking at the source code, it appears that there is a safety check to make sure this size of slice is at all possible:
func makeslice(t *slicetype, len64 int64, cap64 int64) sliceStruct {
// NOTE: The len > MaxMem/elemsize check here is not strictly necessary,
// but it produces a 'len out of range' error instead of a 'cap out of range' error
// when someone does make([]T, bignumber). 'cap out of range' is true too,
// but since the cap is only being supplied implicitly, saying len is clearer.
// See issue 4085.
len := int(len64)
if len64 < 0 || int64(len) != len64 || t.elem.size > 0 && uintptr(len) > maxmem/uintptr(t.elem.size) {
panic(errorString("makeslice: len out of range"))
}
So in this case, it looks like uintptr(len) > maxmem/uintptr(t.elem.size)
so we're not allowed to do this size of an allocation.
However when I allocate struct{}
which takes no memory, this size is allowed:
func main(){
r := make([]struct{}, math.MaxInt64)
fmt.Println(len(r))
}
// prints 9223372036854775807