-1

What is the maximum length of an array that I can declare in Go, Java and C#? Does it relate to the maximum memory at runtime? Or do they have a standard?

Alvin
  • 151
  • 2
  • 16

1 Answers1

1

The Go Programming Language Specification

Array types

An array is a numbered sequence of elements of a single type, called the element type. The number of elements is called the length and is never negative.

The length is part of the array's type; it must evaluate to a non-negative constant representable by a value of type int.

Numeric types

A numeric type represents sets of integer or floating-point values.

There is a set of predeclared numeric types with implementation-specific sizes:

uint     the set of all unsigned integers, either 32 or 64 bits
int      the set of all signed integers, same size as uint

Go array length is a value of type int, which is a 32 or 64 bit signed integer, depending on the compilation architecture (GOARCH), for example, 386 or amd64. It's also subject to any hardware or operating system memory size limits.

package main

import (
    "fmt"
    "runtime"
    "strconv"
)

func main() {
    fmt.Println("int is", strconv.IntSize, "bits on", runtime.GOARCH)
}

Output:

int is 64 bits on amd64
peterSO
  • 158,998
  • 31
  • 281
  • 276