Length vs Capacity | Arrays and Slices
Introduction to GoLang

# Length vs Capacity

We can determine the length of an array or a slice using the `len` function:

index.go

We can determine the capacity of an array or a slice using the `cap` function:

The length and capacity of an array are always the same. However, for a slice, the capacity might differ from the length. For instance, in the case where a slice is created from an array, the capacity is greater than the length:

index.go

The capacity of a slice is the number of elements from the `startingIndex` of the slice to the end of the original array from which it was created. In the example above, it is `6`.

Remember that modifying a slice also modifies the original array. Therefore, in the given example, appending an element (or value) to a slice essentially updates the element next to the slice's ending index in the original array. In other words, it increases the size of the slice and updates the elements of the original array based on the appended elements/values. This will become clearer with an example:

index.go

The capacity is useful for determining how many elements we can append to the slice. If we exceed the capacity of the slice, the `append` function creates and returns a new slice that is not connected to a portion of the original array. Therefore, it becomes detached from the original array.

You can see the following example: the original array remains unchanged even though we appended more elements to its slice:

index.go

This happened because the slice had a capacity of 3, which we exceeded. As a result, the `append` function returned a new slice that no longer references the original array.

What is the correct syntax for appending an element `z` to a slice called `alphabets`?

Everything was clear?

Section 5. Chapter 4

Course Content

Introduction to GoLang

# Length vs Capacity

We can determine the length of an array or a slice using the `len` function:

index.go

We can determine the capacity of an array or a slice using the `cap` function:

The length and capacity of an array are always the same. However, for a slice, the capacity might differ from the length. For instance, in the case where a slice is created from an array, the capacity is greater than the length:

index.go

The capacity of a slice is the number of elements from the `startingIndex` of the slice to the end of the original array from which it was created. In the example above, it is `6`.

Remember that modifying a slice also modifies the original array. Therefore, in the given example, appending an element (or value) to a slice essentially updates the element next to the slice's ending index in the original array. In other words, it increases the size of the slice and updates the elements of the original array based on the appended elements/values. This will become clearer with an example:

index.go

The capacity is useful for determining how many elements we can append to the slice. If we exceed the capacity of the slice, the `append` function creates and returns a new slice that is not connected to a portion of the original array. Therefore, it becomes detached from the original array.

You can see the following example: the original array remains unchanged even though we appended more elements to its slice:

index.go

This happened because the slice had a capacity of 3, which we exceeded. As a result, the `append` function returned a new slice that no longer references the original array.

What is the correct syntax for appending an element `z` to a slice called `alphabets`?