Throughout this exercise, we have introduced concepts and syntax related to using arrays and slices in our Go programs. Let’s take a moment to review what we’ve learned.
An array is a fixed size ordered list of elements with the same data type. Arrays are useful for collecting and accessing multiple related values. Example use cases include:
- Storing sensor values - Computing averages - Holding lists of information
Both arrays and slices are collections of multiple elements of the same data type. However, a slice can be resized to hold additional elements, whereas an array cannot.
An array’s capacity is its length, and this cannot change. A slice has both a length and a capacity, where:
- The slice’s length is the current number of elements it holds
- The slice’s capacity is the number of elements it can hold before needing to resize itself.
Array Creation
There are a variety of ways to create arrays:
An empty array can be created by specifying its number of intended elements and its type.
var myArray [3]int
An array can be created with elements in two ways:
Specifying:
- The number of elements
- The type
- A list of values
animals := [4]string{"Dog", "Hippo", "Cat", "Hamster"}
Or using the ...
syntax to automatically determine the number of elements:
animals := [...]string{"Dog", "Hippo", "Cat", "Hamster"}
Slice Creation
A slice can be created with or without elements.
Without elements, an empty set of square brackets []
and the data type is provided:
var numberSlice []int
With elements, a list of items enclosed in curly braces {}
is also provided:
names := []string{"Kathryn", "Martin", "Sasha", "Steven"}
Array and Slice Access and Modification
Values within an array or slice can be accessed using a square bracket []
syntax:
value := array[index]
Values can be modified using square brackets []
, an index, and the assignment operator =
:
array[index] = value
Array and Slice Functions
The length of an array or slice can be accessed using the len
function.
length := len(sliceOrArray)
The capacity of a slice can be accessed using the cap
function.
capacity := cap(slice)
An element can be added to the end of a slice using the append
function:
slice := append(slice, newElement)
Conclusion
We can use all of this knowledge to collect and modify data values in our programs. Try out a variety of arrays and slices as you create more complex Go programs!
Instructions
Take a look at the workspace for this exercise. It reviews many of the concepts we’ve learned in this lesson. You aren’t required to do anything, but we suggest:
- Modify the array’s content
- Create new arrays
- Add content to slices
- Determine the length of additional arrays
- Change the functions to use strings instead of numbers
- Can you change the functions to both use slices?