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.
Resources
Section titled “Resources”Exercises
Section titled “Exercises”slices1 compile
Section titled “slices1 compile”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.slices2 compile
Section titled “slices2 compile”Take a sub-slice using [low:high] bounds.
Show hint
Slices can be created from arrays using the [low:high] syntax.slices3 compile
Section titled “slices3 compile”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_slicesslices4 test
Section titled “slices4 test”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