Search⌘ K
AI Features

Slice: Length and Capacity

Explore how to determine and manage the length and capacity of slices in Go, including creation with the make() function and efficient appending using the ... operator. Develop a clear understanding of slice properties to write better performing Go code.

About slice length and capacity

Both arrays and slices support the len() function for finding out their length. However, slices also have an additional property called capacity that can be found using the cap() function.

Note: The capacity of a slice is really important when we want to select a part of a slice or when we want to reference an array using a slice.

Slice capacity

The capacity shows how much a slice can be expanded without the need to allocate more memory and change the underlying array. Although the capacity of a ...