O'Reilly logo

Get Programming with Go by Nathan Youngman and Roger Peppé

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required