Golang Array: How to Use Arrays in Golang
Golang programming language provides a data structure called an array, which can store the fixed-size sequential collection of items of the same type.
Golang Array
Golang array is a fixed-size collection of items of the same type. The items of an array are stored sequentially and can be accessed using their index. If we want to declare an array in Go, a programmer specifies the type of the elements and the number of items required by an array.
The array is used to store the collection of data, but it is often more useful to think of the array as the collection of variables of the same type.
Instead of declaring individual variables, such as no1, no2, …, and no99, you declare one array variable such as numbers and use no[0], no[1], and …, no[99] to represent individual variables.
Declaration of Array in Golang
An array belongs to type n[T]. The n denotes the number of elements in an array and T represents the type of each element. Thus, the number of items n is also a part of the kind.
There are different ways to declare arrays. Let’s look at them one by one.
var a[n]T
Here is how you can declare the array of 5 integers.
var a[5]int
See the following code example.
// hello.go package main import ( "fmt" ) func main() { var a [5]int //int array with length 5 fmt.Println(a) }
See the below output.
The var an [5]int declares an integer array of length 5. All elements in the array are automatically assigned a zero value of the array type.
In this case, variable a is the integer array, and hence all items of a are assigned to 0, the zero value of the integer.
An index of the array starts from 0 and ends with length – 1. Let’s assign some values to the above array.
// hello.go package main import "fmt" func main() { var a [5]int a[0] = 18 a[1] = 19 a[2] = 21 a[3] = 29 a[4] = 46 fmt.Println(a) }
Shorthand Declaration of Array in Golang
Let’s create the same array using the shorthand declaration. See the below code.
// hello.go package main import "fmt" func main() { a := [5]int{18, 19, 21, 29, 46} // short hand declaration to create array fmt.Println(a) }
It will give us the same output as the above program.
It is not necessary that all items in the array have to be assigned a value during the shorthand declaration. See the below scenario.
// hello.go package main import "fmt" func main() { a := [5]int{18, 19, 21} // short hand declaration to create array fmt.Println(a) }
In the above code, we have defined array 5 but declared only three items.
In this case, the remaining items have 0 value by default. For example, see the output of the above code.
Array’s length is part of its type.
The size of an array is a part of the type. Hence [15]int and [25]int are distinct types. Because of this, arrays cannot be resized.
Don’t worry about the restriction because slices exist to overcome this disadvantage. However, that means that you cannot resize the array, because resizing the array would mean changing its type, and you are not allowed to change the type of a variable in Golang.
See the below code to understand better.
package main // hello.go func main() { a := [3]int{5, 78, 8} var b [5]int b = a }
If you are using Visual Studio Code and you have installed all the Go extensions, then you might be getting an error inside the VSCode editor. Now, if you run the above code, then it will throw an error.
So, in the above code, variables a and b have different array sizes.
That is why we can not assign each other due to the array length difference, and it is strictly prohibited in Go.
Arrays in Golang are value types.
Arrays in Golang are value types unlike other languages like C, C++, Python, and Java where arrays are reference types.
That means that when you assign the array to the new variable or pass the array to the function, the entire array is copied. So if you want to make any changes to the copied array, the original array won’t be affected and will remain unchanged. See the below code for understanding.
// hello.go package main import "fmt" func main() { arrA := [5]string{"Ned", "Edd", "Jon", "Jeor", "Jorah"} arrB := arrA arrB[1] = "Gendry" fmt.Println("array A = ", arrA) fmt.Println("array B = ", arrB) }
See the output.
In the above code, we have copied the content of arrA to arrB then we have modified the arrB, but still, arrA remains the same, and it does not change.
Length of the array in Go
We can get the length of an array by passing that array as a parameter to the len function in Go. See the below code.
// hello.go package main import "fmt" func main() { data := [...]string{"Ned", "Edd", "Jon", "Jeor", "Jorah"} fmt.Println("length of data is", len(data)) }
See the below output.
Iterating over an array in Golang
You can use a for loop to iterate over the array like the following code.
// hello.go package main import "fmt" func main() { data := [...]string{"Ned", "Edd", "Jon", "Jeor", "Jorah"} for i := 0; i < len(data); i++ { //looping from 0 to the length of the array fmt.Printf("%d th element of data is %s\n", i, data[i]) } }
See the below output.
Iterating over an array using a range operator
Golang provides a more robust form of for loop using the range operator.
Here’s how you can use the range operator with for loop to iterate over the array.
// hello.go package main import "fmt" func main() { data := [...]string{"Ned", "Edd", "Jon", "Jeor", "Jorah"} for index, value := range data { fmt.Printf("Character %d of GoT is = %s\n", index, value) } }
See the below output.
Multidimensional arrays in Golang
Till now, we have dealt with only a one-dimensional array. However, you can also create multi-dimensional arrays in Golang.
The following example demonstrates how to create multidimensional arrays.
// hello.go package main import "fmt" func main() { multiA := [2][2]int{ {19, 21}, {29, 46}, } fmt.Println(multiA) }
See the below code.
That’s it for arrays. The disadvantage of an array is that they come with the restriction that the array’s length is fixed in Go. Thus, it is impossible to increase the length of the array. This is how the Golang slices come into the picture.
In Go language, slices are more common practices in programming than conventional arrays.
Finally, the Golang array example article is over.