Introduction
Slices are a more flexible and powerful way to work with sequences of elements in Go compared to arrays. Unlike arrays, slices are dynamic and can grow and shrink as needed. Slices provide a convenient and efficient way to handle collections of data. In this chapter, you will learn the basics of slices in Go, including how to create, initialize, and manipulate them.
Creating Slices
Creating a Slice from an Array
A slice is created by specifying a range of indices, which selects a portion of an array.
Example:
package main
import "fmt"
func main() {
arr := [5]int{1, 2, 3, 4, 5}
slice := arr[1:4]
fmt.Println(slice) // Output: [2 3 4]
}
Creating a Slice with make
The make
function is used to create a slice with a specified length and capacity.
Syntax:
make([]T, length, capacity)
Example:
package main
import "fmt"
func main() {
slice := make([]int, 3, 5)
fmt.Println(slice) // Output: [0 0 0]
fmt.Println(len(slice)) // Output: 3
fmt.Println(cap(slice)) // Output: 5
}
Creating a Slice with a Slice Literal
A slice literal is used to declare and initialize a slice in a single line.
Example:
package main
import "fmt"
func main() {
slice := []int{1, 2, 3, 4, 5}
fmt.Println(slice) // Output: [1 2 3 4 5]
}
Slicing a Slice
You can create a new slice by slicing an existing slice.
Example:
package main
import "fmt"
func main() {
slice := []int{1, 2, 3, 4, 5}
subSlice := slice[1:4]
fmt.Println(subSlice) // Output: [2 3 4]
}
Appending to a Slice
You can append elements to a slice using the append
function. If the capacity of the slice is exceeded, a new underlying array is allocated.
Example:
package main
import "fmt"
func main() {
slice := []int{1, 2, 3}
slice = append(slice, 4, 5)
fmt.Println(slice) // Output: [1 2 3 4 5]
}
Copying a Slice
You can copy the elements of one slice to another using the copy
function.
Example:
package main
import "fmt"
func main() {
slice1 := []int{1, 2, 3, 4, 5}
slice2 := make([]int, len(slice1))
copy(slice2, slice1)
fmt.Println(slice2) // Output: [1 2 3 4 5]
}
Iterating Over a Slice
You can iterate over a slice using a for
loop or the for-range
loop.
For Loop
Example:
package main
import "fmt"
func main() {
slice := []int{1, 2, 3, 4, 5}
for i := 0; i < len(slice); i++ {
fmt.Println(slice[i])
}
}
For-Range Loop
Example:
package main
import "fmt"
func main() {
slice := []int{1, 2, 3, 4, 5}
for index, value := range slice {
fmt.Printf("Index: %d, Value: %d\n", index, value)
}
}
Slices with Zero Value
A slice with a zero value is nil
, which has a length and capacity of 0.
Example:
package main
import "fmt"
func main() {
var slice []int
fmt.Println(slice) // Output: []
fmt.Println(len(slice)) // Output: 0
fmt.Println(cap(slice)) // Output: 0
fmt.Println(slice == nil) // Output: true
}
Multidimensional Slices
Go supports multidimensional slices, which are slices of slices.
Example:
package main
import "fmt"
func main() {
matrix := [][]int{
{1, 2, 3},
{4, 5, 6},
{7, 8, 9},
}
fmt.Println(matrix)
// Output:
// [[1 2 3]
// [4 5 6]
// [7 8 9]]
}
You can access elements in a multidimensional slice using multiple indices.
Example:
package main
import "fmt"
func main() {
matrix := [][]int{
{1, 2, 3},
{4, 5, 6},
{7, 8, 9},
}
fmt.Println(matrix[1][1]) // Output: 5
matrix[1][1] = 10
fmt.Println(matrix)
// Output:
// [[1 2 3]
// [4 10 6]
// [7 8 9]]
}
Conclusion
Slices in Go are dynamic and flexible data structures that provide powerful ways to handle collections of data. By understanding how to create, initialize, manipulate, and iterate over slices, you can effectively use them in your Go programs. Slices offer more functionality and ease of use compared to arrays, making them an essential tool in Go programming.