[Golang (Go) Tutorial] Arrays
Arrays
An array holds a specific number of elements, and it cannot grow or shrink. Different data types can be handled as elements in arrays such as Int, String, Boolean, and others.
An array is a fixed-length sequence of zero or more elements of a particular type. Because of their fixed length, arrays are rarely used directly in Go. Slices, which can grow and shrink, are much more versatile.
Declaring an Array
To declare an array you need to specify the number of elements it holds in square brackets ([]
), followed by the type of elements the array holds.
An array is created using the var
keyword of a particular type with name, size, and elements.
Syntax:
1 | var array_name[length]Type |
Example:
1 | var intArray [5]int |
Initializing an Array with an Array Literal
Arrays can also declare using shorthand declaration. It is more flexible than the above declaration.
Syntax:
1 | array_name:= [length]Type{item1, item2, item3,...itemN} |
Example:
1 | // Shorthand declaration of array |
Initializing an Array with new keyword
Arrays can also declare and initialize using new
keyword. It will get the point to array.
Syntax:
1 | array_name:= new([length]Type) |
Example:
1 | var a = new([5]int) |
Assign and Access Values
You access or assign the array elements by referring to the index number. The index is specified in square brackets. The index of the first element of any dimension of an array is 0, the index of the second element of any array dimension is 1, and so on.
1 | var theArray [3]string |
Get array length
The length of the array is determined with the len
function.
In the example, we define an array of strings. We print the number of words in the array.
1 | words := [5]string{ "falcon", "sky", "earth", "cloud", "fox" } |
Initializing an Array with ellipses…
When we use ...
instead of specifying the length. The compiler can identify the length of an array, based on the elements specified in the array declaration.
1 | x := [...]int{10, 20, 30} |
Initializing Values for Specific Elements
When an array declare using an array literal, values can be initialize for specific elements.
A value of 10 is assigned to the second element (index 1) and a value of 30 is assigned to the fourth element (index 3).
Example:
1 | x := [5]int{1: 10, 3: 30} |
Loop Through an Indexed Array
You can loop through an array elements by using a for loop.
1 | intArray := [5]int{10, 20, 30, 40, 50} |
Copy Array
Unlike in other languages, array is a value type in Go. This means that when we assign an array to a new variable or pass an array to a function, the entire array is copied.
You can create copy of an array, by assigning an array to a new variable either by value or reference(with &
).
1 | strArray1 := [3]string{"Japan", "Australia", "Germany"} |
Check if Element Exists
To determine if a specific element exist in an array, we need to iterate each array element using for loop and check using if condition.
Example:
1 | func main() { |
Filter Array Elements
You can filter array element using :
as shown below
A value of 10 is assigned to the second element (index 1) and a value of 30 is assigned to the fourth element (index 3).
Example:
1 | countries := [...]string{"India", "Canada", "Japan", "Germany", "Italy"} |
multidimensional arrays
We can create multidimensional arrays in Go. We need additional pairs of square and curly brackets for additional array dimension.
1 | Array_name[Length1][Length2]..[LengthN]Type |
Example:
1 | a := [2][2]int{ |
Compare two arrays
If an array’s element type is comparable then the array type is comparable too, so we may directly compare two arrays of that type using the ==
operator, which reports whether all corresponding elements are equal. The !=
operator is its negation.
1 | a := [2]int{1, 2} |
References
[1] Arrays in Go - GeeksforGeeks - https://www.geeksforgeeks.org/arrays-in-go/
[3] Go array - working with arrays in Golang - https://zetcode.com/golang/array/