Arrays may be indexed by any integer type.
The Array types section of the Go Programming Language Specification says that in an array type definition,
The length is part of the array's type and must be a constant
expression that evaluates to a non-negative integer value.
In an index expression such as a[x]
:
x
must be an integer value and 0 <= x < len(a)
But there is a limitation on the magnitude of an index; the description of Length and capacity says:
The built-in functions len
and cap
take arguments of various types and
return a result of type int
. The implementation guarantees that the
result always fits into an int
.
So the declared size of an array, or the index in an index expression, can be of any integer type (int
, uint
, uintptr
, int8
, int16
, int32
, int64
, uint8
, uint16
, uint32
, uint64
), but it must be non-negative and within the range of type int
(which is the same size as either int32
or int64
-- though it's a distinct type from either).