By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
Penny needs to remember many expenses, not one number. A slice is Go's growable list: an ordered collection of values of the same type. You give it a name, put values in order, and visit them with an index or a loop.
Slices are the workhorse collection of Go. Almost every program you will read uses one, and Penny keeps its expenses in a slice from section 7 onward.
A slice literal writes the element type in square brackets, then the values in braces. []int{1250, 99, 4200} is a slice of three ints. A slice of strings is []string{...}. The empty brackets mean the length is not fixed; a fixed length such as [3]int is an array with different rules, and this course does not need arrays.
package main
import "fmt"
func main() {
expenses := []int{1250, 99, 4200}
fmt.Println("all:", expenses)
fmt.Println("count:", len(expenses))
fmt.Println("first:", expenses[0])
fmt.Println("last:", expenses[len(expenses)-1])
}Notice the brackets before the type and the braces after it: []int{...}. Writing [3]int{...} creates an array with a fixed length. Use the empty form for lists that can grow.
Indexes start at 0, so the first value is at index 0 and the last valid index is len - 1. Reading an index that does not exist stops the program with a runtime panic, so the safe way to reach the last item is expenses[len(expenses)-1].
| Expression | Value | Why |
|---|---|---|
expenses[0] | 1250 | Indexes start at 0. |
expenses[1] | 99 | The second item. |
expenses[2] | 4200 | The last item when len is 3. |
expenses[3] | panic | There is no index 3. |
len(expenses) | 3 | How many items exist. |
The range keyword from step 13 visits every index and value of a slice. for index, category := range categories gives you both. If you only need the value, use _ for the index: for _, cents := range expenses.
package main
import "fmt"
func main() {
categories := []string{"food", "travel", "food"}
for index, category := range categories {
fmt.Printf("%d. %s\n", index+1, category)
}
}The loop prints index+1 so the list starts at 1 for a human reader. The slice itself still starts at index 0; the output is a display choice, not a change to the data.
When you know how long the list should be before you have the values, use make. make([]int, 4) creates four ints, all zero, and len reports 4 straight away. You can then assign each position by index.
package main
import "fmt"
func main() {
totals := make([]int, 4)
totals[0] = 1250
totals[1] = 99
totals[2] = 4200
totals[3] = 300
sum := 0
for _, cents := range totals {
sum += cents
}
fmt.Println("expenses:", totals)
fmt.Println("total cents:", sum)
}Zero values matter here. make([]int, 4) starts as [0 0 0 0], so a sum over it returns 0 for positions you never set. In the program every position is set before the loop runs, and the total is 5849 cents - the same amount formatCents would print as $58.49.
The diagram opens the slice up. A slice value is small: it does not hold the numbers. It holds a pointer to the first element of an underlying array, a length, and a capacity. Indexing and range read through that pointer.
That design is why slices are cheap to pass around, and it is why two slices can share one array - the surprise in step 23. For now, keep the three words in order: pointer, length, capacity.
Create categories := []string{"food", "travel", "rent"}. Print its length, its first item and its last item without changing the slice. Then print each item with its number using range, starting at 1. Expected output: 3, food, rent, then 1. food, 2. travel, 3. rent. As a second step, add a fourth category and confirm the last-item expression still works, because it uses len instead of a hard-coded 3.
Key takeaways
A slice is an ordered, growable list of values of one type: []int{1250, 99, 4200}.
Indexes start at 0; the last valid index is len(slice)-1.
len(...) reports the number of items, and range gives each index and value.
make([]int, n) creates n zero-valued positions you can assign by index.
A slice value holds a pointer to a backing array, a length and a capacity.
Written and edited by the nexGenCoders team. Spotted a mistake? Tell us and we will fix it.
Don't lose this lesson
Create a free account to save your progress, keep your streak, and work toward a shareable certificate.