In Go, you can create a slice from an array by using the slice operator and specifying the start and end indices.
Here's an example:
package main
import "fmt"
func main() {
// Create an array
array := [5]int{1, 2, 3, 4, 5}
// Create a slice from the array
// Note: The slice expression syntax is: array[start:end]
// The start index is inclusive, and the end index is exclusive.
slice := array[1:4]
fmt.Println(slice) // Output: [2 3 4]
}
In this example, we have an array called array
with 5 elements. We then create a slice called slice
using the array.
We use the slice expression array[1:4]
to indicate that the slice should contain elements starting from index 1 (inclusive) up to index 4 (exclusive), which gives us the elements [2, 3, 4] from the array.
Note that the slice does not copy the underlying array but rather references a portion of it. So any modifications made to the slice will also affect the original array.