By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
A list that cannot grow is not very useful. append adds one or more values to the end of a slice and returns the resulting slice. The new list is usually longer by one, and the returned value is what you keep.
append is the function that makes slices growable, and it has one rule that beginners trip over: you must use the returned value. That single rule explains most of the confusing errors around growth.
Look closely at the next program. append does not receive a pointer to your variable; it receives the slice value and returns a new slice value. If you write append(expenses, 300) and ignore the result, your own variable never changes.
package main
import "fmt"
func main() {
expenses := make([]int, 0, 2)
fmt.Println("start:", expenses, "len:", len(expenses), "cap:", cap(expenses))
expenses = append(expenses, 1250)
expenses = append(expenses, 99)
fmt.Println("two items:", expenses, "len:", len(expenses), "cap:", cap(expenses))
expenses = append(expenses, 4200)
fmt.Println("three items:", expenses, "len:", len(expenses), "cap:", cap(expenses))
}The pattern is expenses = append(expenses, value). The right side runs first, and the slice it returns is assigned back to the left side. The exact capacity after growth is a detail of the Go runtime; do not build logic on particular numbers. What you can rely on is that append returns a usable slice and that capacity grows when it must.
The diagram shows the two cases. When the backing array has a free slot, append writes into it and the pointer does not change. When the array is full, Go allocates a larger array, copies the old values across, and returns a slice that points at the new array.
That last sentence is the seed of the next surprise. When append reallocates, the returned slice and any older slice stop sharing an array. When append writes in place, they are still sharing one.
Because a slice is a pointer, length and capacity, two slices can point at the same backing array. expenses[:2] builds a second slice that starts at the same element as expenses and stops before index 2. It does not copy anything.
package main
import "fmt"
func main() {
expenses := []int{1250, 99, 4200}
cheap := expenses[:2]
fmt.Println("before:", expenses, cheap)
cheap[0] = 5
fmt.Println("expenses:", expenses)
fmt.Println("cheap:", cheap)
}Changing cheap[0] changed expenses[0], because both slices point at the same int. This is not a bug; it is the design that makes slicing cheap. The surprise is real: the two names look independent and are not.
When you need two independent slices, allocate the destination with make and use copy. copy(destination, source) copies as many values as both slices can hold and returns how many it copied. After that, changing one slice no longer touches the other.
The usual pattern is two lines: backup := make([]int, len(expenses)) followed by copy(backup, expenses). The make allocates a new array with exactly the right length, and copy fills it with the current values.
The standard library's sort package has sort.Ints and sort.Strings. They sort the slice in place: same slice, same length, new order. Sorting in place is why the copy above matters. If another part of the program still needs the original order, sort a copy.
package main
import (
"fmt"
"sort"
)
func main() {
expenses := []int{4200, 1250, 99}
sorted := make([]int, len(expenses))
copy(sorted, expenses)
sort.Ints(sorted)
fmt.Println("expenses:", expenses)
fmt.Println("sorted:", sorted)
categories := []string{"travel", "food", "rent"}
sort.Strings(categories)
fmt.Println("categories:", categories)
}Read the output with the code: expenses keeps the order it was created with, while sorted is ascending. The categories slice is sorted in place, so its original order is gone - which is fine here, because nothing depended on it.
Create expenses := []int{1250, 99, 4200}. Make a view with cheap := expenses[:2], change cheap[0] to 5, and predict both slices before running. Then build an independent copy with make and copy, change the copy, and confirm the original is unchanged. Last, append a fourth amount to expenses and print len and cap. In one sentence, explain whether the appended slice shares its array with the original and why.
Key takeaways
append(slice, value) returns the new slice; always store the result back.
With spare capacity append writes into the same backing array; when full it allocates a larger array and copies.
The exact capacity after growth is a runtime detail; observe it with cap rather than relying on particular numbers.
Slices that share a backing array change each other: b := a[:2] is a view, not a copy.
copy fills a destination you allocated with make, and sort.Ints / sort.Strings sort in place.
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.