Mahdi.Kh
September 21, 2026
We look at the array data type, get familiar with the operations related to it, and learn how to use it.
Arrays are used to store data of the same type inside a single variable. To define an array, we specify its size and the data type of its items.
In the example above, we defined a variable named x. This variable's type is a numeric (int) array with a length of 3. Because we didn't specify any initial value for the array, the array's values are set to int's initial value, zero (0). So arrays don't need explicit initialization; an array's zero value is a ready-to-use array whose items themselves hold the zero value.
To access the array's values, we can do the following:
In Go, we can access the values inside an array using its index. The index starts at zero (0) and goes up to one less than the array's size. For example, if the array's size is 3, you'll have three indexes: 0, 1, and 2.
In the example above, with indexes 0, 1, and 2, we can access the values inside the array. We can write a value at any index, or read the current value at any index.
If we want to specify an array's initial values when we define it, we can use an array literal, as follows:
If you want to define an array and assign values to only some of its indexes, you can do the following:
In the example above, we only assigned values to the indexes we needed. You can use a colon (:) to specify the index you want. The index goes on the left, and the value you want goes on the right.
If you're using an array literal to set an array's values, you can also specify the array's size as follows:
To compare two arrays, we can use the == or != operators. Two arrays are equal if they have the same size and the same values.
In Go, multi-dimensional arrays can also be simulated as follows:
In the example above, the variable x is an array of length 2, whose elements are each an array holding 3 32-bit floating-point numbers (float32)!
We can't use negative numbers to read from or write to arrays! If we use negative numbers, the program compiles, but it fails at runtime.
And as a final note, whenever you need to get an array's length, you can use the len function. This function takes an array as input and returns its length.
Arrays should only be used when we know the array's length in advance.
In Go, because of the limitations they have, arrays are used very rarely:
An array's size and item type are part of the array's data type, meaning an array defined as [3]int has a different data type than an array defined as [5]int. This means you can't use arrays with different sizes or item types in place of one another.
An array's size must be specified when the array is defined, and it can't be changed after that. Also, a variable can't be used to define an array's size. If we try that, we'll get a compiler error.
Type conversion can't be used to turn an array into an array with a different length.
Arrays are usable right after they're declared, and every index of the array will hold its own zero value if no initial value was assigned to it.
We'll talk more about arrays' limitations in later parts. Here, we just touched on it, and you don't need to fully understand it yet! This was just so you're aware of it.
In Go, arrays are value types. When we assign an array to a variable, a copy of the array's contents is placed in that variable. Changing the array held in that variable has no effect on the original array!
In this part, you got familiar with the array data type and how to define and use it. You saw that you can pass an array to the len function and get its length. In later parts, you'll get a much more complete look at arrays' limitations and see more array examples.
Previous part: Variables and Data Types | Golang basics
Next part: The slice Data Type | Golang basics