I Spy Code - GO

Slice type

A slice is a descriptor for a contiguous segment of an underlying array and provides access to a numbered sequence of elements from that array. A slice type denotes the set of all slices of arrays of its element type. The value of an uninitialized slice is nil.

An array has a fixed size. A slice, on the other hand, is a dynamically-sized, flexible view into the elements of an array.

Here is a simple go lang example that shows how to use a slice.

Source: (example.go)

package main
 
import "fmt"
 
func main() {
   primes := [6]int{2, 3, 5, 7, 11, 13}
 
   var s []int = primes[1:4]
   fmt.Println(s)
}
 

Output:

$ go run example.go
[3 5 7]