ProgramGuru

The append() Function

Start with The append() Function

The built-in append function returns a slice containing the original elements followed by new values. Save the returned slice because its length or backing array may change.

What you’ll learn

  • Explain the main purpose of the append() function.
  • Read and run the complete Go example in this lesson.
  • Apply the concept and recognize common mistakes.

Understand the idea

Write numbers = append(numbers, 7) to add a number. You can append several values or expand another slice with other.... If the current capacity is sufficient, append may reuse the backing array; otherwise it allocates new storage and copies the existing elements.

Do not rely on a particular capacity-growth formula. When you need independence from another slice, explicitly copy your data rather than assuming append always allocates.

Visualize how it works

Follow this diagram before executing the example. Check where a value or decision changes what runs next.

Visual explanation for appendThe returned slice describes the new logical sequence.Start [1,2]Step 1Append 3,4Step 2Append 5,6Step 3Result length=6Step 4The returned slice describes the new logical sequence.
The returned slice describes the new logical sequence.

Run a complete Go example

This example is a complete program. Save it as main.go or paste it into the Go Playground.

Go code

package main

import "fmt"

func main() {
	numbers := make([]int, 2, 4)
	numbers[0], numbers[1] = 1, 2
	numbers = append(numbers, 3, 4)
	extra := []int{5, 6}
	numbers = append(numbers, extra...)
	fmt.Println("Numbers:", numbers)
	fmt.Println("Length:", len(numbers))
}

Output

Numbers: [1 2 3 4 5 6]
Length: 6

Trace what the program does

Appending 3 and 4 fills the original capacity-four backing array. Appending 5 and 6 then requires enough storage for six elements; append returns a usable slice, whether or not it had to allocate. The final length is six.

Avoid common mistakes

  • Always use the slice returned by append; the old descriptor has its previous length.
  • Do not depend on an exact cap() value after allocation; growth details are implementation-dependent.

Try it yourself

Append the integers 7 and 8, and check how the resulting length changes.

Tip: Edit the example above, predict the output, and run the modified code to test your reasoning.

Check your understanding

Why is s = append(s, item) the usual form?
Choose one answer. You can try again.

Answer and explanation

Because append returns the updated slice descriptor

Correct. Appending 3 and 4 fills the original capacity-four backing array.

What you should remember

You have practiced The append() Function in a small, self-contained program. The important habit is to predict what runs, inspect the actual output and distinguish the language rule from the particular example.

Before continuing, make sure you can explain the program to someone who has not seen it, including the line that changes its result.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Copying Slices →