Common Array Operations
Explore common array and slice operations in Go such as accessing, inserting both at the end and middle, updating elements, and deleting items. Understand the performance implications of each operation through time and space complexity analysis to write efficient Go code.
We'll cover the following...
Before writing any algorithm, we need to know what we can actually do with an array-like collection. In Go, we usually use slices for collections that can grow or shrink. Slices support a small, common set of operations. Each one is simple to learn, but together they cover almost everything we will ever need: reading values, adding new ones, changing them, removing old ones, and scanning through the entire collection.
In this lesson, we will go through each operation one by one.
Access (Read an element)
The most basic thing you can do with an array or slice is read a value stored in it. This is called accessing an element. Think of it like looking up a specific game on our scorecard.
Syntax:
To access an element, we write the slice name followed by the index of the element we want to access in square brackets:
array[index]
Example: Consider the following scores slice: []int{88, 95, 70, 82, 91}. If we want to access the score of game 3, we will use scores[2], which will give us 70.
Go implementation:
The following code shows how to access the score at index 2 from the scores slice:
Note: Trying to access an index that does not exist, for example, scores[10] in a five-element slice, will cause a runtime panic.
Insertion (Add an element)
Insertion means adding a new element to the slice. When you insert an element, the slice grows by one. However, not all insertions are equal. The cost of insertion depends onΒ whereΒ the new element goes in the slice.
Insert at the end
Adding an element to the end of the slice is the simplest kind of insertion. Because the new element goes after the current last element, nothing else in the slice needs to move. Think of it like adding a new element to the end of our scorecard and writing the score in.
Syntax:
To append an element to the end in Go, we use the built-in append function and assign the result back to the slice:
array = append(array, value)
Example: Consider a scores slice []int{88, 95, 70, 82}. A fifth game has been played, and the score 91 needs to be recorded. The visualization below shows how a new element is added to the end of the slice:
Go implementation:
The following code adds a new score 91 to the end of the scores slice:
The scorecard originally had four games: []int{88, 95, 70, 82}. A fifth game was played, and the score 91 needs to be recorded. Appending it adds the new score to the end of the scorecard, giving []int{88, 95, 70, 82, 91}. The four previously recorded scores remain exactly where they were.
Insert in the middle or beginning (requires shifting)
If we want to insert an element somewhere other than the end, the computer first needs to create a gap at the right position. To do that, it shifts every element from the insertion point onward one position to the right, and then places the new value into the freed-up slot. This is similar to squeezing a new row into the middle of our scorecard and pushing everything below it down.
Syntax:
Go does not have a separate built-in insert method for slices. A common approach is to combine slicing with append. The index specifies where to place the new element, ...