Skip to content

Exercise 6 of 10 · Slices

Tool Shed Inventory

What you will make

A numbered inventory listing for a tool shed, built from a slice that starts with three tools and gains a fourth.

The one new idea: Slices: literals, growing one with append (and keeping its result), indexing, and len() inside a loop

Slices are Go's everyday list type, and forgetting to keep append's result is one of the most common early mistakes — this exercise makes that mistake visible before it becomes a habit.

Go straight to the code ↓

A slice literal like []string{"hammer", "wrench", "pliers"} gives you a growable list, and append(slice, value) is how you add to it — but append is a plain function, not a method that mutates its argument. It returns the updated slice, and the standard pattern is always slice = append(slice, value). Skip the reassignment and the new element quietly disappears the moment the statement ends, because nothing kept the new slice around.

Worked example

Here's the same append-then-list pattern with two starting tools instead of three:

Go
package main

import "fmt"

func main() {
	tools := []string{"rake", "shovel"}
	tools = append(tools, "hose")

	fmt.Println("Tool shed inventory:")
	for i := 0; i < len(tools); i++ {
		fmt.Printf("%d. %s\n", i+1, tools[i])
	}
	fmt.Println("Total tools:", len(tools))
}
Output
Tool shed inventory:
1. rake
2. shovel
3. hose
Total tools: 3

Three tools print because tools = append(...) actually kept the grown slice, and len(tools) reports 3 as a result.

Your turn

The starter program tries to add a fourth tool to the shed and print a numbered list, but the append call's result is thrown away, and the numbering starts at 0 instead of 1. Fix both marked lines.

If something goes wrong

If your final tool count is one short of what you expect, check that you reassigned append's return value back into tools — that's the single most common slice mistake in Go. If your list starts "0. hammer" instead of "1. hammer", you need i+1 rather than i in the Printf call, since the loop variable itself always starts counting from zero.

Write your code

Runs in your browser. Press Run (or Ctrl/Cmd+Enter) and the output is checked for you.

Ctrl/Cmd+Enter to run

Press Esc then Tab to move keyboard focus out of the code editor.

Ready
Output will appear here after you run your code.

The runtime is starting in the background. You can type now — it will be ready before you are.

The answer appears here once you have run your code at least once.

Things that often go wrong here

Calling append(slice, value) without reassigning the result.
append may or may not grow the underlying array in place, but either way it returns the slice you're meant to keep using — discarding that return value silently throws away the addition.
Assuming a slice's length never changes after it's created.
Unlike a fixed-size array, a slice's len() reflects however many elements it currently holds, which append can increase.
Printing a zero-based loop index as if it were a one-based label.
for i := 0; ... starts at 0, so a human-facing count needs i+1 to start at 1.
Indexing past the end of a slice.
tools[len(tools)] is out of range and panics at runtime — valid indexes only go up to len(tools)-1.

Want a blank editor instead? Open the Go playground.