To get a sub-array in Go, you can use slicing on arrays or slices.
In this example,
numbers
with some values.package main
import "fmt"
func main() {
numbers := []int{1, 2, 3, 4, 5}
subArray := numbers[2:4]
fmt.Println("Sub-array:", subArray)
}
Sub-array: [3 4]
In this example,
colors
with some values.package main
import "fmt"
func main() {
colors := []string{"red", "green", "blue", "yellow", "orange"}
subArray := colors[1:3]
fmt.Println("Sub-array:", subArray)
}
Sub-array: [green blue]
In this tutorial, we learned How to get Sub Array in Go language with well detailed examples.