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 ...

Get Get Programming with Go now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.