Skip to main content

Access Elements of a Slice

Reading a value from a slice works by index: put its position number in square brackets.

Positions are counted from 0, which makes [0] the first element and [1] the second, continuing the same way through the slice.

Example

Printing indexes 0 and 2 shows the first and third values in prices:

Prompt
Create a slice of integers. Access the first and third elements by index.
Output: Go program and captured output

Change Elements of a Slice

Assigning to an index overwrites whatever value is stored there.

Example

The third value in prices goes from 30 to 50:

Prompt
Create a slice of integers. Change the third element, then print the first and third.
Output: Go program and captured output

Append Elements To a Slice

The append() function adds new values after the last element. Assign its result back to the slice, as the syntax shows:

Example

Two new values, 20 and 21, join the end of myslice1. Its length rises from 6 to 8, and its capacity grows to make room:

Prompt
Create a slice of six integers. Append two more elements. Print values, length, and capacity before and after.
Output: Go program and captured output

Syntax

main.go
slice_name = append(slice_name, element1, element2, ...)

Append One Slice To Another Slice

append() can also add a whole slice to the end of another, as the syntax shows.

Note: Those three dots after slice2 are required. They tell Go to pass the elements of the slice one by one.

Example

The values of myslice2 come after those of myslice1, and the combined result goes into myslice3:

Prompt
Create two slices and append one to the other. Print the result, length, and capacity.
Output: Go program and captured output

Syntax

main.go
slice3 = append(slice1, slice2...)

Change The Length of a Slice

An array's length is fixed, but a slice's length can change.

Example

myslice1 first covers four elements of arr1, then is re-sliced down to two. Appending four values takes it to six, which is beyond its capacity of 5, so the capacity grows as well:

Prompt
Create an array, slice it, re-slice to change length, then append items to change length again. Print values, length, and capacity each time.
Output: Go program and captured output

Memory Efficiency

A slice keeps its whole underlying array in memory, even if you only use a small part of it.

So when the array is large and you need just a few of its elements, the better choice is to copy those few with the copy() function.

Copy them into a new slice and that slice gets its own underlying array, holding only what you need. Your program then uses less memory.

Example

numbers holds 15 values, but only the first 5 are needed. They are copied into numbersCopy, created with make(), which has a length and capacity of just 5:

Prompt
Create a large slice, take a sub-slice, and copy it to a new slice using copy(). Print both the original and the copy with length and capacity.
Output: Go program and captured output

Syntax

copy() takes two slices, a destination dest and a source src, and copies data from the source into the destination. Its return value tells you how many elements were copied.

main.go
copy(dest, src)