slice is not an array. A slice describes a piece of an array.

A slice is a descriptor of an array segment. It consists of:

  • the pointer to the array
  • the length of the segment
  • the capacity (the maximum length of the segment).

Slicing (s = s[2:4]) does not copy the slice’s data. It creates a new slice value that points to the original array. This makes slice operations as efficient as manipulating array indices. Therefore, modifying the *elements *(not the slice itself) of a re-slice modifies the elements of the original slice:

To increase the capacity of a slice one must create a new, larger slice and copy the contents of the original slice into it.

https://go.dev/blog/slices-intro


🌱 Back to Garden