// The following shows some different slices, with information on them given below
var s []int
t := []int{}
u := make([]int, 5)
v := make([]int, 0, 5)
w := []int{1,2,3,4,5}
arrAddr is a pointer to the underlying arrays is an uninitialised or nil slice
arrAddrt is an initialised but empty slice
arrAddr points to a special sentinel struct{} value (again an internal thing that is basically a nothing value but not nil)u is an initialised slice with 5 length and 5 capacity
v is an initialised slice with 0 length and 5 capacity
a[0:2] - which will take the 0 and 1 elements of a (it is exclusive for the to side)0:2, you get back a slice descriptor with length 2 but capacity 5 (since the underlying array is the same and has length 5)0:3 and you’ll get back a slice descriptor of length 3 - which will contain the value at index 2 of the original slice!!!a[0:2:2] - this will create a slice descriptor of length 2 AND CAPACITY 2
b := [2]string{"Hello", "world"}, and you can do b := [...]string to let Go determine the size of the array for you based on the proceeding literalmake function (func make([]T, len, cap) []T)len and cap functions can be used to retrieve the length and capacity of a slicearr and create a slice referencing (or providing a view of) the storage of arr using s := arr[:]s = s[:cap(s)]s := make([]int, 5)
// This is basically the internal implementation of slice growing that Go uses when appending to a slice that has reached it's max capacity
t := make([]int, len(s), (cap(s)+1)*2)
copy(t, s)
s = t
append(s []T, x ...T) []T... operator to expand the second arg into a list of args
append(s, x...) for s []T and x []T
func filter(s []int, fn func(int) bool) {
var res []int // == nil
for _, v := range s {
if fn(v) {
res = append(res, v)
}
}
return res
}