Introduction
Slicing lets you take a part of a list or array to work with just that piece. It helps you focus on smaller parts without changing the original whole.
slice[low:high] slice[low:] slice[:high] slice[:]
numbers := []int{10, 20, 30, 40, 50} part := numbers[1:4] fmt.Println(part)
letters := []string{"a", "b", "c", "d"}
start := letters[2:]
fmt.Println(start)chars := []string{"x", "y", "z"}
end := chars[:2]
fmt.Println(end)all := chars[:] fmt.Println(all)
package main import "fmt" func main() { fruits := []string{"apple", "banana", "cherry", "date", "fig"} firstTwo := fruits[:2] middle := fruits[1:4] last := fruits[3:] all := fruits[:] fmt.Println("First two fruits:", firstTwo) fmt.Println("Middle fruits:", middle) fmt.Println("Last fruits:", last) fmt.Println("All fruits:", all) }
arr[2:5] do?arr[2:5]arr from the beginning up to index 4 (excluding 4), omitting the start index?arr[:4] omits the start index (defaults to 0), slicing up to but excluding index 4. arr[0:4] explicitly includes the start index.arr := []int{10, 20, 30, 40, 50}
slice := arr[1:4]
fmt.Println(slice)arr[1:4] includes elements at indexes 1, 2, and 3.arr := []int{1, 2, 3, 4, 5}
slice := arr[3:2]
fmt.Println(slice)
What is the issue here?arr[3:2] has start index 3 and end index 2.nums := []int{5, 10, 15, 20, 25, 30}, which expression produces a new slice containing only the last three elements?nums[3:] slices from index 3 to the end, giving [20 25 30].Pass the test and this pattern moves unknown