Lesson 18. A bigger slice
After reading lesson 18, you’ll be able to
- Append more elements to slices
- Investigate how length and capacity work
Arrays have a fixed number of elements, and slices are just views into those fixed-length arrays. Programmers often need a variable-length array that grows as needed. By combining slices and a built-in function named append, Go provides the capabilities of variable-length arrays. This lesson delves into how it works.
Consider this
Have you ever had your books outgrow your shelves, or your family outgrow your home or vehicle?
Like bookshelves, arrays have a certain capacity. A slice can focus on the portion of the array where the books are, and grow to reach the capacity of the shelf. If the shelf is full, you can replace the shelf with a larger one and move all the books over. Then point the slice at the books on the new shelf with a greater capacity.
The International Astronomical Union (IAU) recognizes five dwarf planets in our solar system, but there could be more. To add more elements to the dwarfs slice, use the built-in append function as shown in the following listing.
Listing 18.1. More dwarf planets: append.go
dwarfs := []string{"Ceres", "Pluto", "Haumea", "Makemake", "Eris"} dwarfs = append(dwarfs, "Orcus") fmt.Println(dwarfs) #1
The append function is variadic, like Println, so you can pass multiple elements to append in one go: