Arrays in Go

Arrays are fixed-size collections of elements of the same type. In Go, the size is part of the array’s type.

Declaration

To declare an array, you specify the size and the type.

var myInts [10]int

Initialized Literals

You can also initialize an array with values immediately.

primes := [6]int{2, 3, 5, 7, 11, 13}

Key Characteristics

  • Fixed Size: Once declared, you cannot change the size of an array.
  • Value Type: Arrays in Go are value types. When you assign an array to a new variable or pass it to a function, a complete copy of the array is made.
  • Zero Values: Elements are initialized to their type’s zero value (e.g., 0 for int, "" for string).