Junior — Middle
What actions can be performed using slicing in a programming language?
sobes.tech AI
Answer from AI
In the Go language, slices allow working with sequences of elements of dynamic size. With slices, you can:
- Obtain subsets of an array or another slice (slice a part of the data).
- Add elements using the
appendfunction. - Modify elements by index.
- Pass slices to functions for working with dynamic collections.
Example of using a slice:
package main
import "fmt"
func main() {
arr := [5]int{1, 2, 3, 4, 5}
s := arr[1:4] // slice of elements with indices 1, 2, 3
fmt.Println(s) // [2 3 4]
s = append(s, 6) // add an element
fmt.Println(s) // [2 3 4 6]
}