Slices
A slice is a stretchy list.
An array has a fixed size. A slice can grow.
Almost all list-like data in Go uses slices.
Think of a stretchy shopping list. You can add more items later.
Creating slices
You can make a slice with a list of values.
You can also use make, or cut a piece from an array.
len is how many items you have now.
cap is how much room is left before Go needs more space behind the list.
append adds items
append adds items to the stretchy list.
It may make a bigger space behind the list when room runs out.
Always save the result: s = append(s, x).
Indexing
Read and write items with brackets, just like arrays.
Indexes start at 0.
Slicing a slice
The expression s[low:high] makes a new stretchy list view.
Both views can share the same space behind the list.
Be careful. Changing items can change both slices.
Copying safely
If you need a true separate list, copy the items.
Then changes to one list do not touch the other.
Use slices for lists. Use arrays only when the size is fixed and part of the type.
Test yourself
Three quick questions made just for this lesson. Earn 10 XP per correct answer.