Go edit


Slice is a growable sequence of values of the same type.

Other languages call them arrays or vectors.

Memory used by slice is provided by a fixed size array. A slice is a view into that array.

Slice has length and capacity.

Capacity represents how many total elements a slice can have. That’s the size of underlying array.

Length is the current number of elements in the slice.

The difference between capacity and length is how many elements we can append to a slice before we have to re-allocate underlying array.

Zero value of a slice is nil.

Basic of slices

slice := make([]int, 0, 5)
// append element to end of slice
slice = append(slice, 5)
// append multiple elements to end
slice = append(slice, 3, 4)
fmt.Printf("length of slice is: %d\n", len(slice))
fmt.Printf("capacity of slice is: %d\n", cap(slice))
length of slice is: 3
capacity of slice is: 5

Feedback about page:

Optional: your email if you want me to get back to you:

  ↑ ↓ to navigate     ↵ to select     Esc to close