For this second installment of Getting Going With Go, we’re going to cover
slices – how to create them, how to add more elements to them, and how to retrieve the elements of them individually. We’ll also cover how what
maps are in Go and how to create them.
Slices in Go are an abstraction built on top of arrays. They consist of a pointer to the array they refer to, the length of the part of the array they represent, and the maximum length of the part of the array they represent. If you’re not careful, it’s easy to confuse a slice for an array because the syntax is so similar. For example:
It’s important to remember that for an array, the number of elements in the array is a part of the type definition and must be included whereas for a slice, because it is dynamically sized, the length is not a part of the definition.
The way to declare a new slice without assigning values to it is different also.
make(Type, length, capacity) allocates an array and returns a slice that refers to that array. The length argument is to set the initial length of the underlying array. The capacity argument is an optional argument and refers to the maximum length of the array.
When you initialize a slice this way, without using the
make function, the capacity and slice are the same.
Slicing an array
Additionally, in order to inspect the length and capacity of a slice, we can use the built-in
It might seem confusing that the capacity of
b is 4 while
c both have a capacity of 6 like the original array
s. The reason for this is that the capacity of a slice is the number of elements in the underlying array, counting from the first element in the slice. Because
b in this example starts at the 2nd index, it has two fewer elements in capacity.
Extending a Slice
We can achieve the same functionality in Go by using the
The other way to make a slice larger is by using the builtin
append function, which adds elements to the end of a slice.
func copy(dst, src T) int copies the contents of the src slice into the destination slice. Copy returns the number of elements copied, which will be the minimum of len(src) and len(dst)
func append(slice Type, elems ...Type) Type adds elements to the end of the slice. If there is enough capacity to accomodate the new elements appended, it reslicing to accomodate the new elements. If there is not enough capacity, a new array is allocated.
append returns the updated slice, which is why it is important to store the result of append.
groceries ...string in the arguments combined the two strings passed into the function into a slice. Then in the append function, the
groceries... separated them back out into a comma-separated list of values.
Like what happens with a slice when you declare it without assigning values and then try to assign values to it, assigning to a map will result in a panic. And like with a slice, in order to be able to assign values to a map, you need to use the
This can also be accomplished with shorthand syntax
In order to assign the value from a map to a variable:
However, this actually returns two values – the value of the selected key in the map and a boolean indicating whether or not that key exists in the map.
To delete a key from a map: