ProgramGuru

Introduction to Slices

Start with Introduction to Slices

A slice is a flexible view of a sequence of elements. Unlike an array, its length is not part of its type, and a slice can grow using append.

What you’ll learn

  • Explain the main purpose of introduction to slices.
  • Read and run the complete Go example in this lesson.
  • Apply the concept and recognize common mistakes.

Understand the idea

A slice type uses empty brackets: []int. A literal such as []int{2, 4, 6} creates a slice containing three integers. Internally, a slice value describes a portion of an underlying array: a reference to its elements, a length and a capacity.

Assigning one slice to another copies the descriptor, not all its elements. Until an operation reallocates storage, the two slices can refer to the same underlying elements. That distinction is central to correct Go code.

Visualize how it works

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

Visual explanation for slicesSlice variables may share storage after assignment.values sliceStep 1Copy descriptorStep 2Both view same arrayStep 3Write via aliasStep 4Slice variables may share storage after assignment.
Slice variables may share storage after assignment.

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() {
	values := []int{2, 4, 6}
	alias := values
	alias[0] = 20
	fmt.Println("Values:", values)
	fmt.Println("Alias:", alias)
	fmt.Println("Length:", len(values))
}

Output

Values: [20 4 6]
Alias: [20 4 6]
Length: 3

Trace what the program does

Both variables initially describe the same underlying elements. Assigning 20 through alias[0] also changes the first element observed through values. The length remains three.

Avoid common mistakes

  • A slice assignment does not independently copy all elements.
  • A slice is not an array with a secret fixed length; len and cap are separate concepts.

Try it yourself

Create a slice of three names and assign it to another variable. Change one element through the second variable and inspect both.

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

Check your understanding

What does assigning one ordinary slice to another copy?
Choose one answer. You can try again.

Answer and explanation

Only a slice descriptor referencing the same elements

Correct. Both variables initially describe the same underlying elements.

What you should remember

You have practiced Introduction to Slices 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: Creating Slices →