Slices in Go are a crucial and versatile data structure that provides more flexibility and power than arrays. They are built on top of arrays and allow dynamic resizing, making them ideal for many applications. Understanding slice internals, operations, and how to work with multidimensional slices is essential for any Go developer.
A slice in Go is a descriptor of an array segment. It consists of three components:
gos := make([]int, 5, 10)
fmt.Println(len(s)) // Output: 5
fmt.Println(cap(s)) // Output: 10
make
function creates a slice with a specified length and capacity.Slicing creates a new slice by referencing a subrange of an existing array or slice.
goarr := [5]int{1, 2, 3, 4, 5}
slice := arr[1:4]
fmt.Println(slice) // Output: [2 3 4]
slice := arr[1:4]
creates a slice including elements from index 1 to index 3 (excluding index 4).Appending adds elements to the end of a slice. If there is insufficient capacity, a new array is allocated.
gos := []int{1, 2, 3}
s = append(s, 4, 5)
fmt.Println(s) // Output: [1 2 3 4 5]
append
function returns a new slice with the additional elements.The copy
function copies elements from a source slice to a destination slice.
gosrc := []int{1, 2, 3}
dst := make([]int, len(src))
copy(dst, src)
fmt.Println(dst) // Output: [1 2 3]
copy
function returns the number of elements copied, which is the minimum of the lengths of the source and destination slices.To grow a slice beyond its current capacity, appending causes a reallocation of the underlying array.
gos := make([]int, 0, 2)
s = append(s, 1, 2)
fmt.Println(len(s), cap(s)) // Output: 2 2
s = append(s, 3)
fmt.Println(len(s), cap(s)) // Output: 3 4
Multidimensional slices are slices of slices. They allow the creation of complex data structures like matrices.
gomatrix := make([][]int, 3) // Create a slice of 3 slices
for i := range matrix {
matrix[i] = make([]int, 3) // Each inner slice has 3 elements
}
matrix[0][0] = 1
fmt.Println(matrix) // Output: [[1 0 0] [0 0 0] [0 0 0]]
Elements in a multidimensional slice are accessed and modified using nested indexing.
gomatrix := [][]int{
{1, 2, 3},
{4, 5, 6},
{7, 8, 9},
}
matrix[1][2] = 10
fmt.Println(matrix) // Output: [[1 2 3] [4 5 10] [7 8 9]]
Appending to inner slices works the same way as with single-dimensional slices.
gomatrix := [][]int{
{1, 2, 3},
{4, 5},
}
matrix[1] = append(matrix[1], 6)
fmt.Println(matrix) // Output: [[1 2 3] [4 5 6]]
Slices in Go provide a powerful and flexible way to handle collections of data. They are built on arrays but offer dynamic resizing and more convenient operations. Understanding slice internals, performing slice operations like slicing, appending, and copying, and working with multidimensional slices are crucial skills for effective Go programming. By mastering these concepts, you can write more efficient and maintainable Go code.