A Complete Guide to Arrays in Golang
What is an Array in Go?
In Go, an array is a fixed-length collection of elements of the same type. The elements of an array are stored in contiguous memory locations. Arrays are zero-indexed, meaning the first element of an array is at index 0. The length of an array is part of its type, so arrays of different lengths are considered different types.
Arrays and slices are both used to store multiple values in a single variable. The difference between the two is that arrays have a fixed length, while slices have a dynamic length. Slices are built on top of arrays.
Creating an Array in Go
Arrays in Go are created using the var
keyword followed by the array name, the array length, and the type of the elements. The syntax for creating an array is var arrayName [length]type
.
Here, we have declared an array named arr
that will hold 4 integers. The type of the elements is int
and the array length is 4. The elements of an array are indexed starting from zero.
Here we have assigned values to the elements of the array. We can also declare and initialize an array in a single line; this is known as an array literal. As shown below.
Accessing Array Elements in Golang
Array elements can be accessed using the index of the element. The index of the first element is 0, and the index of the last element is the length of the array minus 1.
Iterating Over an Array in Golang
Array elements can be accessed using a for loop. The index of the first element is 0, and the index of the last element is the length of the array minus 1.
The output of the above code is:
We could also use the range
keyword to iterate over the elements of an array.
The output of the above code is:
The range
keyword returns both the index and the value of the element. We
can use the blank identifier _
to ignore the index.
Array Length in Golang
Arrays have the len
function that returns the length of the array.
Since arrays are fixed-length, the length of an array cannot be changed after it is created.
Array Slicing in Golang
Array slicing is a technique to access a subset of an array. The syntax for array slicing is array[start:end]
. The start
index is inclusive, and the end
index is exclusive. The start
index defaults to 0, and the end
index defaults to the length of the array.
Array Comparison in Golang
Arrays can be compared using the ==
operator. The ==
operator returns true if the two arrays have the same length and the same elements in the same order.
The last print statement returns false because the elements of arr1
and arr3
do not contain the same values.
Array Sorting in Golang
Arrays can be sorted using the sort
package. The sort
package provides the sort.Ints
function that sorts an array of integers in ascending order.
Another tedious way of sorting an array is to use for
loops. The following code sorts an array of integers in ascending order.
Array Copy in Golang
Arrays can be copied using the copy
function. The copy
function takes two arguments: the destination array and the source array. The copy
function returns the number of elements copied.
Array Delete in Golang
Elements of an array can be deleted by setting the value of the element to the zero value of the type of the element. The zero value of an integer is 0.
Array Contains in Golang
To check if array contains a value, we can use the sort.Search
function from the sort
package. The sort.Search
function takes three arguments: the length of the array, a function that returns true if the element at the given index is greater than or equal to the value, and the value to search for.
The output of the above code is:
Array Reverse in Golang
Arrays can be reversed in two ways. The first way is to replace the first element with the last element, the second element with the second last element, and so on. The second way is to use the sort.Reverse
function from the sort
package.
Multidimensional Arrays in Golang
Arrays can have more than one dimension. A multidimensional array is an array of arrays. The syntax for declaring a multidimensional array is var arr [rows][columns]type
. The rows
and columns
are the number of rows and columns in the multidimensional array, and type
is the type of the elements in the multidimensional array.
Most of the operations that can be performed on a 1D array can also be performed on a multidimensional array.
2D Array in Golang
A 2D array is an array of arrays. The syntax for declaring a 2D array is var arr [rows][columns]type
. The rows
and columns
are the number of rows and columns in the 2D array, and type
is the type of the elements in the 2D array.
The above code declares a 2D array with 2 rows and 3 columns. The elements of the 2D array are integers.
Accessing 2D Array Elements in Golang
2D array elements can be accessed using the row and column index of the element. The row index of the first element is 0, and the column index of the first element is 0. The row index of the last element is the number of rows minus 1, and the column index of the last element is the number of columns minus 1.
2D Array Length in Golang
The len
function returns the number of rows in a 2D array. The len
function does not return the number of columns in a 2D array. However, we can loop through the rows of a 2D array and use the len
function to get the number of columns in each row.
Array of Slices in Golang
An array of slices is an array where each element is a slice. The syntax for declaring an array of slices is var arr [size]type
, where size
is the number of elements in the array, and type
is the type of the elements in the array.
The above code declares an array of slices with 2 elements. The elements of the array are slices of integers.
Array of Maps in Golang
An array of maps is one in which each element is a map. The syntax for declaring an array of maps is var arr [size]type
.
The above code declares an array of maps with 2 elements. The elements of the array are maps with string keys and integer values.
Array of Functions in Golang
An array of functions is one where each element is a function. The syntax for declaring an array of functions is var arr [size]type
.
The above code declares an array of functions with 2 elements. The elements of the array are functions that take an integer as an argument and return an integer.
Array of Interfaces in Golang
An array of interfaces is one where each element is an interface. The syntax for declaring an array of interfaces is var arr [size]type
.
The above code declares an array of interfaces with 2 elements. The elements of the array are an integer and a string.
Array of Pointers in Golang
An array of pointers is one where each element is a pointer. The syntax for declaring an array of pointers is var arr [size]type
.
The above code declares an array of pointers with 2 elements. The elements of the array are pointers to integers.
Array of Channels in Golang
An array of channels is one where each element is a channel. The syntax for declaring an array of channels is var arr [size]type
.
The above code declares an array of channels with 2 elements. The elements of the array are channels of integers.
Array of Structs in Golang
An array of structs is one where each element is a struct. The syntax for declaring an array of structs is var arr [size]type
.
The above code declares an array of structs with 2 elements. The elements of the array are Person
structs.
Thats all about arrays in Golang. I hope you found this guide helpful. If you have any questions or feedback, feel free to reach out to me.