Slices
Context
Section titled “Context”A slice is a dynamically‑sized, flexible view into an array. Unlike arrays, slices are reference types and are used much more often. They consist of a pointer, length, and capacity.
Example
Section titled “Example”Create slices using literals, make, and slicing.
Code example
Section titled “Code example”package main
import "fmt"
func main() { s1 := []int{1, 2, 3} // slice literal s2 := make([]int, 3, 5) // length 3, capacity 5 s3 := s1[1:3] // slice from index 1 to 2
s2[0] = 10 s1 = append(s1, 4, 5) // grows dynamically
fmt.Println(s1, len(s1), cap(s1)) fmt.Println(s2, len(s2), cap(s2)) fmt.Println(s3)}Output
Section titled “Output”[1 2 3 4 5] 5 8[10 0 0] 3 5[2 3]