Golang Array Basics

Glitch
3 min readMar 11, 2023

--

Arrays are an essential data structure in any programming language. They are a collection of homogeneous elements with a fixed size. In Golang, arrays are also available as a built-in data type. In this blog, we will explore arrays in Golang, how to create and manipulate them, and some of their use cases.

Creating an array in Golang
In Golang, we can create an array using the following syntax:
var arrayName [size]dataType
Here, the arrayName is the name of the array, size is the number of elements in the array, and dataType is the type of elements in the array.

For example, to create an array of integers with five elements, we can use the following code:
var myArray [5]int
In this code, we have created an array called myArray with five elements of type int.

Initializing an array in Golang
We can initialize an array while creating it by specifying the values for each element. For example, we can initialize the myArray with the following values:
var myArray [5]int = [5]int{1, 2, 3, 4, 5}
We can also initialize an array by specifying values for some elements and leaving others with their default values. For example:
var myArray [5]int = [5]int{1, 2, 3}
In this case, the first three elements of myArray will have values 1, 2, and 3, respectively. The last two elements will have their default value, which is 0 for integers.

Accessing elements of an array in Golang
We can access the elements of an array using the index. The index starts from 0 and goes up to size-1. For example, to access the second element of myArray, we can use the following code:
fmt.Println(myArray[1])
In this code, we have used the index 1 to access the second element of myArray.

Iterating over an array in Golang
We can use a for loop to iterate over an array in Golang. For example, the following code iterates over myArray and prints its elements:
for i := 0; i < len(myArray); i++ {
fmt.Println(myArray[i])
}
In this code, we have used the len function to get the size of myArray.

Multidimensional arrays in Golang
Golang also supports multidimensional arrays, which are arrays of arrays. We can create a two-dimensional array using the following syntax:
var arrayName [size1][size2]dataType
Here, size1 is the number of rows, size2 is the number of columns, and dataType is the type of elements in the array.

For example, to create a two-dimensional array of integers with three rows and two columns, we can use the following code:
var myArray [3][2]int
We can access the elements of a multidimensional array using two indices. For example, to access the element at row i and column j of myArray, we can use the following code:
fmt.Println(myArray[i][j])
Conclusion
Arrays are an essential data structure in Golang. They are useful for storing and manipulating a collection of elements with a fixed size. In this blog, we have explored how to create and manipulate arrays in Golang, including initializing, accessing, and iterating over them. We have also looked

--

--

No responses yet