An array is a data structure or a collection of an ordered sequence of elements[1] that belong together.
Arrays enable condensing the code and performing the same methods and operations on multiple values at once[2].
Contents
Abstract
Introduction
Definition and declaration
Indexing
Counting elements
Modifying elements
Summary
References
Introduction
In Golang, an array refers to a continuous memory of a segment that stores elements of the same data type.
Its capacity is defined at creation time which can no longer be changed. If the capacity is unknown during
creation time, a programmer should take the privilege of using slices. While slices have the dynamic capacity,
it comes at a cost - less performant.
Unlike slices, arrays are not reference or pointer types. It saves the actual values.
Arrays are hashable which can be used as keys in maps and this makes it comparable.
Index bounds are checked at compile-time that gives the array higher compile-time safety.
1
2
a:= [5]int{}
a[5] = 10// compile-time error: invalid array index 5 (out of bounds for 5-element array)
Assigning array values makes a copy of the entire array implicitly.
1
2
3
a:= [5]int{1, 2, 3, 4, 5}
b:=ab[2] = 10// it affects only b; a remains the same
Arrays are quite useful for sequences of fixed size, e.g. IP addresses. For IPv4, [4]byte provides a
compile-time guarantee that the value passed to the function will have exactly 4 bytes, while for IPv6
[16]byte provides the compile-time guarantee.
It is wise to use arrays where the data structure does not need a dynamic number of elements since one-time
memory allocation increases the speed and performance of the program besides many other advantages.
Definition and declaration
General schema for declaring an array:
[size]data_type{elements}
All elements must be the same data type, e.g. int or byte or string, etc.
If the values of the elements are not declared, the default is zero-valued.
1
2
3
a:= [5]int{} // a = [0 0 0 0 0]
b:= [5] byte{} // b = [0 0 0 0 0]
c:= [5] string{} // c = [ ]
If an array stored in a variable is printed out with Println() function, sometimes it is hard to
understand the values of each element:
1
2
3
msg:= [5]string{"hello world", "mars", "hello saturn", "jupiter"}
fmt.Println(msg)
// output: [hello world mars hello saturn jupiter ]
With Printf() function and %q verb this problem can be solved:
Each element in an array corresponds to an index number starting from 0 and counting up. In Golang,
it is not allowed to index backward with a negative number.
1
2
3
4
5
fmt.Println(msg[0]) // hello world
fmt.Println(msg[1]) // mars
fmt.Println(msg[2]) // hello saturn
fmt.Println(msg[3]) // jupiter
fmt.Println(msg[4]) //
It is possible to concatenate the same data types.
1
2
fmt.Println(msg[0] +" and hello from "+msg[1])
// output: hello world and hello from mars
Counting elements
With the built-in len() function, the length of the array can be found.
1
fmt.Println(len(msg)) // 5
Modifying elements
Indexing provides greater control over the data in an array by allowing changing the elements.
Note: It is not possible to change the size or capacity of an array once it is defined.
Therefore, only an existing element can be changed, but a new element cannot be added or
removed from the array.
Summary
This tutorial demonstrates how array works, discusses the advantages and limitations.
When the size of the data structure is known, an array can act as a powerful tool to
increase the overall performance of the program. Slices play an amazing role when
data structure size is unknown.
Reference
1. 6.2 Array Types - Computer Science Programming Basics in Ruby [Book], accessed March 6, 2021, https://www.oreilly.com/library/view/computer-scienceprogramming/9781449356835/sixdot2_array_types.html
2. i Gopher Guides, Understanding Arrays and Slices in Go, DigitalOcean, accessed March 6, 2021, https://www.digitalocean.com/community/tutorials/understanding-arrays-and-slices-in-go