Skip to content

Slices

Beginner · Collections & Loops

A slice is a dynamically-sized, flexible view into an underlying array. Create with make([]T, len, cap) or a literal, grow with append, and take sub-slices with s[low:high]. Slices have a length and a capacity.

Create a slice with make, giving its element type.

Show hint
Slices can be created with the `make` function and must have a specified type.

Source

Take a sub-slice using [low:high] bounds.

Show hint
Slices can be created from arrays using the [low:high] syntax.

Source

Add elements to a slice with append.

Show hint
Use the `append` function to add elements to an existing slice.
Check the output after adding some elements.
For more on the `append` function, refer to: https://go.dev/ref/spec#Appending_and_copying_slices

Source

Index a slice within its bounds.

Show hint
Check the slice indices to ensure they fall within the bounds of the names slice.
For more information: https://go.dev/ref/spec#Appending_and_copying_slices

Source