Go: Slices

Declaration of Slice

Syntax:

[]T
or 
[]T{}
or 
[]T{value1, value2, value3, ...value n}

Here, T is the type of the elements. For example:

var my_slice[]int

Components of Slice

Three components of a slice:

  • Pointer: The pointer is used to points to the first element of the array that is accessible through the slice. Here, it is not necessary that the pointed element is the first element of the array.
  • Length: The length is the total number of elements present in the array.
  • Capacity: The capacity represents the maximum size up to which it can expand.

Leave a Reply

Your email address will not be published. Required fields are marked *