Slices have both length and capacity. The length of a slice is the number of elements currently in the slice, while the capacity is the number of elements the slice can hold before needing to be reallocated.

When creating a slice using the built-in make() function, you can specify its length, and optionally its capacity. You can check capacity and length with len() and cap():

https://codeeval.dev/gist/3c64ee8d85acb6b567b9550474ebf167

If the capacity is not explicitly specified, it will default to the value of the specified length.

https://codeeval.dev/gist/7f4625c49808ce6c78f91c0df4328feb

Elements created by make() are set to the zero value for the element type of the slice:

https://codeeval.dev/gist/3889946447fe274e6a83de42399e709b

You cannot access elements beyond the length of a slice, even if the index is within capacity:

https://codeeval.dev/gist/2e58d44d8a41bb56822f7f5f56c4e283

Capacity allow us to optimize performance.