Understanding Slices in Go
Slices in Go are a powerful abstraction over arrays. In this post, we explore how slices work, their syntax, and key behaviors.
In Go, a slice is a flexible and powerful wrapper over an array. It allows dynamic sizing and is used extensively in Go programs.
๐น Declaring a Slice
You can create a slice using a slice literal:
numbers := []int{1, 2, 3, 4, 5}This is a slice of integers. Unlike arrays, you donโt specify the length.
๐น Slicing an Array
Slices can also be created from an array:
arr := [5]int{10, 20, 30, 40, 50}
slice := arr[1:4] // elements 20, 30, 40Slice syntax: array[start:end] โ includes start, excludes end
๐น Length and Capacity
Slices have length and capacity:
fmt.Println(len(slice)) // number of elements
fmt.Println(cap(slice)) // number of elements from start to end of underlying array๐น Modifying Slices
Slices are references. Modifying a slice affects the underlying array:
arr := [3]int{1, 2, 3}
slice := arr[:]
slice[0] = 99
fmt.Println(arr) // [99 2 3]๐น Using append()
You can add elements using append:
nums := []int{1, 2}
nums = append(nums, 3, 4)
fmt.Println(nums) // [1 2 3 4]Appending beyond capacity may allocate a new array.
๐น Copying Slices
Use copy() to copy slices:
src := []int{1, 2, 3}
dest := make([]int, len(src))
copy(dest, src)
fmt.Println(dest) // [1 2 3]โ ๏ธ Common Gotchas
- Appending to a slice may detach it from the original array
- Always check
len()andcap()when slicing
โ Summary
- Slices are dynamic, reference-based views over arrays
- They simplify working with collections
- Use
append,copy, and slicing syntax to manipulate them efficiently
Go slices are essential to idiomatic Go programming. Understanding how they work under the hood will help you write more efficient and safe code.
Happy slicing! ๐ฐ