|
|
|
// declare a slice - similar to an array, but length is unspecified
|
|
|
|
var a []int
|
|
|
|
// declare and initialize a slice (backed by the array given implicitly)
|
|
|
|
var a = []int {1, 2, 3, 4}
|
|
|
|
// shorthand
|
|
|
|
a := []int{1, 2, 3, 4}
|
|
|
|
// ["a", "b", "c"]
|
|
|
|
chars := []string{0:"a", 2:"c", 1: "b"}
|
|
|
|
|
|
|
|
// creates a slice (view of the array) from index lo to hi-1
|
|
|
|
var b = a[lo:hi]
|
|
|
|
// slice from index 1 to 3
|
|
|
|
var b = a[1:4]
|
|
|
|
// missing low index implies 0
|
|
|
|
var b = a[:3]
|
|
|
|
// missing high index implies len(a)
|
|
|
|
var b = a[3:]
|
|
|
|
// append items to slice a
|
|
|
|
a = append(a,17,3)
|
|
|
|
// concatenate slices a and b
|
|
|
|
c := append(a,b...)
|
|
|
|
|
|
|
|
// create a slice with make
|
|
|
|
a = make([]byte, 5, 5) // first arg length, second capacity
|
|
|
|
a = make([]byte, 5) // capacity is optional
|
|
|
|
|
|
|
|
// create a slice from an array
|
|
|
|
x := [3]string{"Лайка", "Белка", "Стрелка"}
|
|
|
|
s := x[:] // a slice referencing the storage of x
|