Go Slices and Arrays Explained Simply

Updated 4 min read
Go Slices featured image: cards comparing an array, a slice and slicing

Go slices are the most common way to store a list of values, like items in a cart. In this post, you will learn how slices and arrays work and how to grow them. You will also learn to avoid their classic traps. Each idea comes with a short example that you can run yourself.

You already looped over a slice with range in the lesson on loops in Go with range, break and continue. You also passed one into a function in writing and calling functions in Go. Now it is time to see what a slice really is.

Arrays: The Fixed-Size List

var scores [3]int
scores[0] = 90
fmt.Println(scores, len(scores)) // [90 0 0] 3

days := [3]string{"Mon", "Tue", "Wed"}
fmt.Println(days) // [Mon Tue Wed]

An array holds a fixed number of values of one type. The size is part of its type, so [3]int can never grow to four items. Empty spots get the zero value, which is why you see 0 0 above.

Think of an egg tray with exactly six holes. It is handy, but you cannot add a seventh egg. Because of this limit, you will rarely use arrays directly.

What Are Go Slices?

A slice is a flexible view of an array that can grow as you add items. So it works like a shopping bag rather than an egg tray. In real projects, Go slices are what you will use almost every time.

Creating Go Slices

cart := []string{"milk", "bread"}

nums := make([]int, 3, 5)
fmt.Println(nums, len(nums), cap(nums)) // [0 0 0] 3 5

var empty []int
fmt.Println(empty == nil, len(empty)) // true 0

Notice the empty brackets in []string. No size means it is a slice, not an array. The make function creates a slice with a starting length and room to grow. Also, a slice you never fill is nil, which simply means empty.

Adding Items with append

cart = append(cart, "eggs", "rice")
fmt.Println(cart, len(cart)) // [milk bread eggs rice] 4

The append function adds items to the end and returns the new slice. That is why you must save the result back into cart. Even a nil slice works with append, so you can start empty and grow.

Length and Capacity of Go Slices

nums := make([]int, 3, 5)
nums = append(nums, 7)
fmt.Println(nums, len(nums), cap(nums)) // [0 0 0 7] 4 5

Length, from len, is how many items the slice holds right now. Capacity, from cap, is how much room the hidden array has. When length reaches capacity, append quietly creates a bigger array and copies everything over.

How Go Slices Work Inside

Every slice is a small header with three parts: a pointer to an array, a length and a capacity. A pointer is just the address of where the data lives. As a result, two slices can point at the same array.

Slicing a Slice

letters := []string{"a", "b", "c", "d", "e"}
part := letters[1:4]
fmt.Println(part, len(part), cap(part)) // [b c d] 3 4

part[0] = "X"
fmt.Println(letters) // [a X c d e]

The expression letters[1:4] takes items from index 1 up to, but not including, index 4. However, it does not copy anything. So changing part[0] also changes letters[1], as the diagram shows.

Go Slices diagram showing two slice headers sharing one underlying array

Both slices share one array, so a change through part is visible in letters.

Making a Safe Copy

src := []int{1, 2, 3}
dst := make([]int, len(src))
copy(dst, src)
dst[0] = 100
fmt.Println(src, dst) // [1 2 3] [100 2 3]

When you need an independent list, use copy. It fills a new slice with the values, so later changes stay separate.

Looping Over Go Slices

for i, item := range cart {
	fmt.Println(i, item)
}
// 0 milk
// 1 bread
// 2 eggs
// 3 rice

Range gives you the index and the value for each item. If you only need the value, replace i with an underscore.

Common Mistakes with Go Slices

Error you seeWhy it happensHow to fix it
append(c, “b”) (value of type []string) is not usedThe result of append was thrown away.Write c = append(c, “b”).
panic: runtime error: index out of range [0] with length 0The code read an item from an empty slice.Check len(s) first.
Data changes by surpriseTwo slices share one array.Use copy for a separate list.

The shared array trap catches even experienced developers. For that reason, interviewers love it, and you will find similar questions in my Go interview questions and answers.

Official Resources

These pages come straight from the Go team, so they are always accurate and up to date.

Quick Recap

  • Arrays have a fixed size, such as [3]int.
  • Go slices grow with append, and you must save the result.
  • Length is how many items exist, and capacity is the room left in the array.
  • Slicing with s[1:4] shares the same array, so changes show in both.
  • Use copy when you need a fully separate list.

What’s Next?

Slices store items by position. Next, we will store values by name using maps, which work like a phone book.

Coming soon: Go Maps Explained with Simple Examples